SlideShare uma empresa Scribd logo
1 de 57
By: Dabbal S. Mahara
2018
Relational Model
• Relational data model first introduced by Ted Codd of IBM in 1970.
• It attracted attention due to its simplicity and mathematical foundation.
• First commercial implementations of the relational model became available
in the early 1980s, such as the SQL/DS system on MVS operating system by
IBM and the Oracle DBMS.
• Current popular DBMSs include:
 DB2 and Informix Dynamic Server ( from IBM)
 Oracle and Rdb (from Oracle)
 SQL Server and Access (from Microsoft)
2
Relational Model
• The relational model represents the database as a collection of relations.
• A relation is a table of values, each row in the table represents a
collection of related data values.
• Each row in the table represents a fact that typically corresponds to a
real-world entity or relationship.
• The table name and column names are used to help to interpret the
meaning of values in each row.
3
Relational Model
 Data are organized in two-dimensional tables called relations.
 The tables are related to each other.
 The most popular model.
4
Relational Model
• RDBMS (Relational Database Management System)
• external view
• The data are represented as a set of relations.
• A relation is a two-dimensional table.
• This doesn’t mean that data are stored as tables; the physical
storage of the data is independent of the way the data are
logically organized.
5
Relational Model
• In the formal relational model terminology, a row is called a tuple, a
column header is called an attribute, and the table is called a relation.
• The data type describing the types of values that can appear in each
column is represented by a domain of possible values.
• For example:
• Name: the set of character strings that represent names of persons.
• Employee_age: the possible ages of employees of a company; each must be an
integer between18 to 60.
• Grade_point: possible values of computed grade point average; each must a real
number between 0 to 4.
6
Relation
 Name – each relation in a relational database should have a name that is unique
among other relations.
 Attribute – each column in a relation.
 The degree of the relation – the total number of attributes for a relation.
 Tuple – each row in a relation.
 The cardinality of the relation – the total number of rows in a relation.
7
Attributes
• An attribute Ai is the name of a role played by some domain D in the
relational schema R.
• A relational schema R, is denoted by R(A1, A2, A3, ..., An ), is made of a
relation name R and list of attributes A1, A2,......., An )
• The degree (or arity ) of a relation is the number of attributes n of its
relation schema.
• For example: the following relation schema represents a relation of degree
seven and describes university students,
student(Name, Ssn, Home_phone,Address, Office_phone, Age, Gpa)
8
Tuple
• A relation r of relation schema R(A1, A2 , A3, ..., An ), also denoted by r(R),
is a set of n-tuples, r = { t1 , t2 , t3 ,... , tn}.
• Each n-tuple t is an ordered list of n values t = < t1 , t2 , t3 ,... , tn >.
• The ith value of n-tuple t corresponds to the attribute Ai .
• The terms relation intension for schema R and relation extension for a
relation state r(R) are commonly used.
9
10
Relational Model Constraints
 There are generally many restrictions or constraints on the actual values in a
database state.
 These constraints are derived from the rules in the miniworld that the database
represents.
 Constraints on the database can generally be divided into three main categories.
 Inherent model-based or implicit constraints: For example: the relation cannot have
duplicate tuples is an inherent constraint.
 Schema based constraints or explicit constraints: These constraints are expressed in
schema of relational model via the DDL. For example: domain constraints and Keys
 Application based or semantic constraints or business rules: functional dependency or
multivalued dependencies.
11
Domain Constraints
 Domain constraints specify that within each tuple, the
value of each attribute A must be an atomic value from
the domain dom(A).
 This is specified as data types which includes standard
numeric data types integers, real numbers, characters,
Booleans, fixed length strings, variable length strings etc.
 These are specified in DDL statements.
12
KEY Constraints
Definition
◦ A Column value in the table that uniquely identifies a single record in the table is called key of a
table.
◦ An attribute or the set of attribute in the table that uniquely identifies each record in the entity set is
called a key for that entity set
Types of keys
◦ Simple Key: A key which has the single attribute is known as a simple key.
◦ Composite key: A key which consist two or more attributes is called a Composite Key.
Example:
Cust_id is a key attribute of Customer Table it is possible to have a single key for one customer i.e is
Cust_id ie Cust_id =1 is only for the Cust_name =”Yogesh”.Please refer to the Customer Table which is
mentioned below.
13
Types of keys
Types of Key Definition of Key
Super Key A key is called super key which is sufficient to identify the
unique record in the table
Candidate Key A minimal super key is called candidate key. A super key that
has no proper subset of candidate key.
Primary Key A candidate key is chosen as a principal to identify a unique.
Foreign Key An column ( or combination of columns) in the one table
whose values is match the primary key in the another table
14
Customer table
Cust_id Cust_Name Cust_Age Cust_Address Cust_Mobile_N
o
Cust_Phone_No
1 Yogesh 20 Kathmandu 9841567081 4434999
2 Ramesh 23 Pokhara 9985555522 4443434
3 Ram 18 Chitwan 9854333332 656565
4 Pramod 24 Dailekh 9848044401 34343434
5 Yashu 25 Birendranagar 9848080000 44545454
6 Sarawsati 23 Gulmi 9855555555 93434343
15
Super Key and key
 Any set of attributes SK is called superkey of the relation schema R, if
for any two distinct tuples t1 and t2 in the relation state r of R, we have
the constraint that t1[SK] ≠ t2[SK].
 A key k of a relation schema R is a superkey of R with the additional
property that removing any attribute A from K leaves a set of attributes
K’ that is not a superkey of R any more.
 Hence a key satisfies two constraints:
1. Two distinct tuples in any state of the relation cannot have
identical values for all the attributes in the key.
2. It is a minimal superkey – that i, a superkey from which we cannot
remove any attributes and still have the uniqueness constraints in
condition 1 hold.
16
Super key and key
 Example:
 Cust_id attribute of the entity set Customer uniquely identifies
Customer entity from another, so the Cust_id is the Super key.
 Another way is, the combination of Cust_id attribute and Cust_Name
attribute is the Super key for the Customer Entity set.
 Only the Cust_Name is not called the Super Key because several
customer may have the Same Name
 Cust_id is called key as it is minimal super key.
17
Candidate Keys
 In general a relation schema may have more than one key.
 In this case, each of them is called a candidate key.
 For example, the CAR relation has two candidate keys: License_Number
and Engine_serial_Number.
 It is common to designate one of the candidate keys as the primary key.
 This is the Candidate key whose values are used to identify tuples in a
relation.
License_Nu
mber
Engine_serial_Numbe
r
Make Model Year
18
NOT NULL and Entity Integrity Constraints
 NOT-NULL constraint:
It is a constraint on attributes whether null values are or are not permitted. For
example, if every Customer tuple must be valid, non-null value for the cust_Name
attribute, then cust_Name of customer is constrained to be NOT NULL.
 Entity Integrity Constraint:
It states that no primary key value can be null.
This is because the primary key value is used to identify individual tuples in a relation.
 Key constraints and entity integrity constraints are specified on individual relations.
19
Referential Integrity Constraint
 The referential Integrity Constraints are specified between two relations and is
used to maintain the consistency among tuples in the two relations.
 It states that a tuple in one relation that refers to another relation must refer to
an existing tuple in that relation.
 To define the referential integrity more formally, the concept of foreign key is
needed.
 All the integrity constraints should be specified on the relational database
schema if we want to enforce these constraints on the database state.
 A database state that does not obey all the integrity constraints is called an
invalid state.
20
Foreign Key
 A set of attributes FK is relation schema R1 is a foreign key of R1 that
references relation R2 if it satisfies the following rules:
 The attributes in FK have the same domain(s) as the primary key
attributes PK or R2
 A value of FK in a tuple t1 of the current stare r1(R1) either occurs as a
value of PK for some tuple t2 in the current state r2(R2) or is NULL.
i.e. t1[FK] = t2[PK] and we say that the tuple t1 references or refers
to the tuple t2.
 In this definition R1 is referencing relation and R2 is referenced relation.
 If these two conditions hold, a referential integrity constraint from R1 to R2 is
said to hold.
21
Example: Referential Integrity ( Company database)
22
23
Example: Referential Integrity
 In the EMPLOYEE relation, the attribute Dno refers to the department for
which an employee works; hence, we designate Dno to be a foreign key
of EMPLOYEE referencing the DEPARTMENT relation.
 This means that a value of Dno in any tuple t1 of the EMPLOYEE relation
must match a value of the primary key of DEPARTMENT—the Dnumber
attribute—in some tuple t2 of the DEPARTMENT relation, or the value of
Dno can be NULL if the employee does not belong to a department or will
be assigned to a department later.
 For example, in above tables, the tuple for employee ‘John Smith’
references the tuple for the ‘Research’ department, indicating that ‘John
Smith’ works for this department.
24
Other Constraints
1. Semantic integrity constraints:
 These are constraints specified and enforced within the application programs that
update the database, or by using general purpose constraint specification language.
 For example: salary of an employee should not exceed the salary of the employee’s
supervisor and maximum number of hours an employee can work on all projects per
week is 56.
 Mechanisms called triggers and assertions can be used.
2. Functional dependency constraint:
 It establishes a functional relationship among two sets of attributes X and Y.
 This constraint specifies that the value of X determines the value of Y in all states of a
relation; it is denoted as a functional dependency X -> Y.
25
Database Operations on relational Model
 The operations of the relational model can be categorized
into retrievals and updates.
 The relational algebra operations can be used to specify
retrievals.
 A relational algebra expression forms a new relation after
applying a number of algebraic operators to an existing
set of relations; its main use is for querying a database.
 This relation becomes the answer to the user’s query.
 The relational calculus also defines new relation
declaratively without giving a specific order of operations.
26
Update operations
 There are three basic update operations on relations:
insert, delete and modify.
 insert is used to insert a new tuple or tuples in a relation.
 Delete is used to delete tuples.
 Update or Modify is used to change the values of some
attributes in existing tuples.
 Whenever these operations are applied, integrity
constraints specified on the relational database schema
should not be violated.
27
Insert Operation
 The insert operation provides a list of attribute values for a new
tuple t that is to be inserted into a relation R.
 Insert can violate any of the four constraints discussed above.
 Domain constraints can be violated if an attribute value is given
that does not appear in the corresponding domain or is not of the
appropriate data type.
 Key constraints can be violated if a key value in the new tuple t
already exists in another tuple in the relation r(R).
 Entity integrity can be violated if any part of the primary key of
the new tuple t is NULL.
 Referential integrity can be violated if the value of any foreign
key in t refers to a tuple that does not exist in the referenced
relation.
28
Insert operation
 An unary operation.
 Insert a new tuple into the relation.
29
The Delete Operation
 The Delete operation can violate only referential integrity.
 This occurs if the tuple being deleted is referenced by foreign keys
from other tuples in the database.
 To specify deletion, a condition on the attributes of the relation
selects the tuple (or tuples) to be deleted.
 In general, when a referential integrity constraint is specified in the
DDL, the DBMS will allow the database designer to specify which of
the options applies in case of a violation of the constraint.
30
The Delete operation
 An unary operation.
 Delete a tuple defined by a criterion from the relation.
31
The Update Operation
 The Update (or Modify) operation is used to change the values of one
or more attributes in a tuple (or tuples) of some relation R.
 It is necessary to specify a condition on the attributes of the relation
to select the tuple (or tuples) to be modified.
 Updating an attribute that is neither part of a primary key nor of a
foreign key usually causes no problems; the DBMS need only check to
confirm that the new value is of the correct data type and domain.
 Modifying a primary key value is similar to deleting one tuple and
inserting another in its place because we use the primary key to
identify tuples.
32
The Update operation
 An unary operation.
 Changes the value of some attributes of a tuple.
33
Relational Algebra and Relational Calculus
 These are the two formal languages for relational model.
 A data model must include a set of operations to manipulate the database, in
addition to the data model’s concepts for defining database structures and
constraints.
 The basic set of operations for the relational model is the relational algebra. These
operations enable a user to specify basic retrieval requests. The result of a
retrieval is a new relation, which may have been formed from one or more
relations, which can be further manipulated using operations of same relational
algebra.
 The relation algebra is very important for serval reasons.
 First, it provides a formal foundation for relational model operations.
 Second, it is used as a basis for implementing and optimizing queries in RDBMS.
 Third, the core operations and functions of any relational systems are based on
relational algebra operations.
34
Relational Algebra and Relational Calculus
 Relational calculus provides a higher-level declarative notation for
specifying relational queries.
 In a calculus expression, there is no order of operations to specify how
to retrieve the query result – a calculus expression specifies only what
information the result should contain.
 Relational calculus expression creates a new relation, which is
specified in terms of variables that range over rows of the stored
database ( in tuple relational calculus) over columns of the stored
relations ( in domain calculus).
 The relational calculus is important because it has firm basis in
mathematical logic and the standard query language (SQL) has some
of its foundation in tuple relational calculus.
35
Relational Algebra
 The relational algebra is often considered to be an integral part of the
relational data model.
 Its operations can be divided into two groups.
 One group includes set operations from mathematical set theory; these
are applicable because each relation is defined to be a set of tuples in
the formal relational model. Set operations include UNION,
INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT (also known
as CROSS PRODUCT).
 The other group consists of operations developed specifically for
relational databases—these include SELECT, PROJECT, and JOIN, among
others.
36
The select operation
 The SELECT operation is used to choose a subset of the tuples from a
relation that satisfies a selection condition.
 One can consider the SELECT operation to be a filter that keeps only
those tuples that satisfy a qualifying condition.
 For example, to select the EMPLOYEE tuples whose department is 4, or
those whose salary is greater than $30,000, we can individually specify
each of these two conditions with a SELECT operation as follows:
σDno=4(EMPLOYEE)
σSalary>30000(EMPLOYEE)
37
The select Operation
 In general, the SELECT operation is denoted by
σ<selection condition> (R)
 where the symbol σ (sigma) is used to denote the SELECT operator and the
selection condition is a Boolean expression (condition) specified on the
attributes of relation R.
 Notice that R is generally a relational algebra expression whose result is a
relation—the simplest such expression is just the name of a database relation.
 The relation resulting from the SELECT operation has the same attributes as
R.
 For example, to select the tuples for all employees who either work in
department 4 and make over $25,000 per year, or work in department 5 and
make over $30,000, we can specify the following SELECT operation:
σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)
38
The Select operation
 An unary operation.
 It is applied to one single relation and creates another
relation.
 The tuples in the resulting relation are a subset of the
tuples in the original relation.
 Use some criteria to select
39
The Project Operation
 The PROJECT operation selects certain columns from the table and
discards the other columns.
 If we are interested in only certain attributes of a relation, we use
the PROJECT operation to project the relation over these attributes
only.
 Therefore, the result of the PROJECT operation can be visualized as
a vertical partition of the relation into two relations:
 One has the needed columns (attributes) and contains the result of
the operation, and the other contains the discarded columns.
 For example, to list each employee’s first and last name and salary,
we can use the PROJECT operation as follows:
πLname, Fname, Salary(EMPLOYEE)
40
The Project Operation
 The general form of the PROJECT operation is
π<attribute list> (R)
 where π (pi) is the symbol used to represent the PROJECT operation,
and < attribute list > is the desired sub list of attributes from the
attributes of relation R.
 Again, notice that R is, in general, a relational algebra expression
whose result is a relation, which in the simplest case is just the name
of a database relation.
 The result of the PROJECT operation has only the attributes specified
in <attribute list> in the same order as they appear in the list.
 Hence, its degree is equal to the number of attributes in <attribute
list>.
41
The Project Operation
 An unary operation.
 It is applied to one single relation and creates another
relation.
 The attributes in the resulting relation are a subset of the
attributes in the original relation.
42
Rename Operation
 In general, for most queries, we need to apply several relational algebra
operations one after the other. Either we can write the operations as a single
relational algebra expression by nesting the operations, or we can apply one
operation at a time and create intermediate result relations.
 In the latter case, we must give names to the relations that hold the
intermediate results.
 For example, to retrieve the first name, last name, and salary of all employees
who work in department number 5, we must apply a SELECT and a PROJECT
operation.
 We can write a single relational algebra expression, also known as an in-line
expression, as follows:
πFname, Lname, Salary (σDno=5(EMPLOYEE))
 Alternatively, we can explicitly show the sequence of operations, giving a name
to each intermediate relation, as follows:
 DEP5_EMPS ← σDno=5(EMPLOYEE)
 RESULT ← πFname, Lname, Salary(DEP5_EMPS) 43
Relational Algebra Operations from Set
Theory
 Several set theoretic operations are used to merge the elements of two sets in
various ways, including UNION, INTERSECTION, and SET DIFFERENCE (also called
MINUS or EXCEPT).
 These are binary operations; that is, each is applied to two sets (of tuples).
 When these operations are adapted to relational databases, the two relations on
which any of these three operations are applied must have the same type of
tuples; this condition has been called union compatibility or type compatibility.
 Two relations R(A1, A2, ..., An) and S(B1, B2, ..., Bn) are said to be union
compatible (or type compatible) if they have the same degree n and if dom(Ai) =
dom(Bi) for 1 <=i<= n.
 This means that the two relations have the same number of attributes and each
corresponding pair of attributes has the same domain.
44
Relational Algebra Operations from Set
Theory
 We can define the three operations UNION, INTERSECTION, and SET
DIFFERENCE on two union-compatible relations R and S as follows:
 UNION: The result of this operation, denoted by R ∪ S, is a relation that
includes all tuples that are either in R or in S or in both R and S. Duplicate
tuples are eliminated.
 INTERSECTION: The result of this operation, denoted by R ∩ S, is a
relation that includes all tuples that are in both R and S.
 SET DIFFERENCE (or MINUS): The result of this operation, denoted by R –
S, is a relation that includes all tuples that are in R but not in S.
45
Example
 For example, to retrieve the Social Security numbers of all employees
who either work in department 5 or directly supervise an employee
who works in department 5, we can use the UNION operation as
follows:
 DEP5_EMPS ← σDno=5(EMPLOYEE)
 RESULT1 ← πSsn(DEP5_EMPS)
 RESULT2 ← πSuper_ssn(DEP5_EMPS)
 RESULT ← RESULT1 ∪ RESULT2
46
Union operation
 A binary operation.
 Creates a new relation in which each tuple is either in
the first relation, in the second, or in both.
 The two relations must have the same attributes.
47
Intersection operation
 A binary operation.
 Creates a new relation in which each tuple is a member
in both relations.
 The two relations must have the same attributes.
48
The Difference operation
 A binary operation.
 Creates a new relation in which each tuple is in the first relation but
not the second.
 The two relations must have the same attributes.
49
Cartesian Product
 This is also a binary set operation, but the relations on which it is applied do
not have to be union compatible.
 In its binary form, this set operation produces a new element by combining
every member (tuple) from one relation (set) with every member (tuple) from
the other relation (set).
 In general, the result of R(A1, A2, ..., An) × S(B1, B2, ..., Bm) is a relation Q
with degree n + m attributes Q(A1, A2, ..., An, B1, B2, ..., Bm), in that order.
 The resulting relation Q has one tuple for each combination of tuples—one
from R and one from S.
 Hence, if R has nR tuples (denoted as |R| = nR), and S has nS tuples, then R × S
will have nR * nS tuples.
50
The Join Operation
 The JOIN operation, denoted by , is used to combine related tuples
from two relations into single “longer” tuples.
 The JOIN operation can be specified as a CARTESIAN PRODUCT
operation followed by a SELECT operation.
 Suppose that we want to retrieve the name of the manager of each
department. To get the manager’s name, we need to combine each
department tuple with the employee tuple whose Ssn value matches
the Mgr_ssn value in the department tuple.
• Note that Mgr_ssn is a foreign key of the DEPARTMENT relation that
references Ssn, the primary key of the EMPLOYEE relation. This
referential integrity constraint plays a role in having matching tuples
in the referenced relation EMPLOYEE.
51
The join operation
 The general form of a JOIN operation on two relations R(A1, A2, ..., An) and
S(B1, B2, ..., Bm) is
R ⋈ <join condition>S
 The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, ..., An, B1,
B2, ... , Bm) in that order; Q has one tuple for each combination of tuples—one
from R and one from S—whenever the combination satisfies the join condition.
 This is the main difference between CARTESIAN PRODUCT and JOIN. In JOIN,
only combinations of tuples satisfying the join condition appear in the result,
whereas in the CARTESIAN PRODUCT all combinations of tuples are included in
the result.
 The join condition is specified on attributes from the two relations R and S and is
evaluated for each combination of tuples.
 Each tuple combination for which the join condition evaluate to TRUE is included
in the resulting relation Q as a single combined tuple.
52
The Join operation
 A binary operation.
 Combines two relations based on common attributes.
53
Variations of Join
 Theta Join: produces all combinations of tuples form R1 and R2 that
satisfy the join condition
 Equijoin: Produces all the combinations of tuples from R1 and R2 that
satisfy a join condition with only equality comparisons.
 Natural Join: Same as equijoin except that the join attributes of R2
are not included in the resulting relation; if the join attributes have
the same names, they do not need to be specified at all.
 In general, NATURAL JOIN is performed by equating all the attribute
pairs that have the same name in the two relations.
54
Example:
 The join of two relations creates a new relation. The notation R1 x R2
indicates the join of relations R1 and R2. For example, consider the
following:
Relation R1
A B C
1 5 3
2 4 5
8 3 5
9 3 3
1 6 5
5 4 3
2 7 5
Relation R2
B D E
4 7 4
6 2 3
5 7 8
7 2 3
3 2 2
Relation R1 x R2
A B C D E
1 5 3 7 8
2 4 5 7 4
8 3 5 2 2
9 3 3 2 2
1 6 5 2 3
5 4 3 7 4
2 7 5 2 3
The row (2 4 5 7 4) was formed by joining the row (2 4 5) from relation R1 to the row (4
7 4) from relation R2. The two rows were joined since each contained the same value for
the common attribute B. The row (2 4 5) was not joined to the row (6 2 3) since the
values of the common attribute (4 and 6) are not the same.
55
Inner and Outer Joins
 INNER joins contain only matches.
 OUTER joins may contain mismatches as well.
 Inner Join:-
This is sometimes known as a simple join. It returns all rows from both tables where
there is a match. If there are rows in R1 which do not have matches in R2, those rows
will not be listed.
 Left Outer Join:-
Returns all the rows from R1 even if there are no matches in R2. If there are no
matches in R2 then the R2 values will be shown as null.
 Right Outer Join:-
Returns all the rows from R2 even if there are no matches in R1. If there are no
matches in R1 then the R1 values will be shown as null.
 Full Outer Join:-
Returns all the rows from both tables even if there are no matches in one of the tables.
If there are no matches in one of the tables then its values will be shown as null.
56
Thank you !
57

Mais conteúdo relacionado

Mais procurados (20)

ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
Database Normalization
Database NormalizationDatabase Normalization
Database Normalization
 
Data Models
Data ModelsData Models
Data Models
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
Data dictionary
Data dictionaryData dictionary
Data dictionary
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Database abstraction
Database abstractionDatabase abstraction
Database abstraction
 
Schema
SchemaSchema
Schema
 
Database language
Database languageDatabase language
Database language
 
ER MODEL
ER MODELER MODEL
ER MODEL
 
Enhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) ModelingEnhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) Modeling
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
 
RELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptxRELATIONSHIP IN DBMS.pptx
RELATIONSHIP IN DBMS.pptx
 
Relational database
Relational database Relational database
Relational database
 
Dbms 14: Relational Calculus
Dbms 14: Relational CalculusDbms 14: Relational Calculus
Dbms 14: Relational Calculus
 

Semelhante a Relational model

Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...Mustafa Kamel Mohammadi
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]Bashir Rezaie
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdfLPhct2
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptxThangamaniR3
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...Raj vardhan
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPTssuser3e0f731
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Prosanta Ghosh
 
Relational model
Relational modelRelational model
Relational modelRUpaliLohar
 
Relational model
Relational modelRelational model
Relational modelRUpaliLohar
 

Semelhante a Relational model (20)

Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
The relational data model part[1]
The relational data model part[1]The relational data model part[1]
The relational data model part[1]
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptx
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
 
Relational model
Relational modelRelational model
Relational model
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
DBMS Unit-2.pdf
DBMS Unit-2.pdfDBMS Unit-2.pdf
DBMS Unit-2.pdf
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPT
 
Relational Model
Relational ModelRelational Model
Relational Model
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
 
RDBMS
RDBMSRDBMS
RDBMS
 
unit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdfunit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdf
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
NMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.pptNMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.ppt
 
Dbms relational data model and sql queries
Dbms relational data model and sql queries Dbms relational data model and sql queries
Dbms relational data model and sql queries
 

Mais de Dabbal Singh Mahara (20)

Temporal databases
Temporal databasesTemporal databases
Temporal databases
 
Spatial databases
Spatial databasesSpatial databases
Spatial databases
 
Ordbms
OrdbmsOrdbms
Ordbms
 
Odbms concepts
Odbms conceptsOdbms concepts
Odbms concepts
 
Object database standards, languages and design
Object database standards, languages and designObject database standards, languages and design
Object database standards, languages and design
 
Normalization
NormalizationNormalization
Normalization
 
Mobile databases
Mobile databasesMobile databases
Mobile databases
 
Active database
Active databaseActive database
Active database
 
Deductive databases
Deductive databasesDeductive databases
Deductive databases
 
Overview of dbms
Overview of dbmsOverview of dbms
Overview of dbms
 
ER modeling
ER modelingER modeling
ER modeling
 
EER modeling
EER modelingEER modeling
EER modeling
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 

Último

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...Amil Baba Dawood bangali
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Último (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 

Relational model

  • 1. By: Dabbal S. Mahara 2018
  • 2. Relational Model • Relational data model first introduced by Ted Codd of IBM in 1970. • It attracted attention due to its simplicity and mathematical foundation. • First commercial implementations of the relational model became available in the early 1980s, such as the SQL/DS system on MVS operating system by IBM and the Oracle DBMS. • Current popular DBMSs include:  DB2 and Informix Dynamic Server ( from IBM)  Oracle and Rdb (from Oracle)  SQL Server and Access (from Microsoft) 2
  • 3. Relational Model • The relational model represents the database as a collection of relations. • A relation is a table of values, each row in the table represents a collection of related data values. • Each row in the table represents a fact that typically corresponds to a real-world entity or relationship. • The table name and column names are used to help to interpret the meaning of values in each row. 3
  • 4. Relational Model  Data are organized in two-dimensional tables called relations.  The tables are related to each other.  The most popular model. 4
  • 5. Relational Model • RDBMS (Relational Database Management System) • external view • The data are represented as a set of relations. • A relation is a two-dimensional table. • This doesn’t mean that data are stored as tables; the physical storage of the data is independent of the way the data are logically organized. 5
  • 6. Relational Model • In the formal relational model terminology, a row is called a tuple, a column header is called an attribute, and the table is called a relation. • The data type describing the types of values that can appear in each column is represented by a domain of possible values. • For example: • Name: the set of character strings that represent names of persons. • Employee_age: the possible ages of employees of a company; each must be an integer between18 to 60. • Grade_point: possible values of computed grade point average; each must a real number between 0 to 4. 6
  • 7. Relation  Name – each relation in a relational database should have a name that is unique among other relations.  Attribute – each column in a relation.  The degree of the relation – the total number of attributes for a relation.  Tuple – each row in a relation.  The cardinality of the relation – the total number of rows in a relation. 7
  • 8. Attributes • An attribute Ai is the name of a role played by some domain D in the relational schema R. • A relational schema R, is denoted by R(A1, A2, A3, ..., An ), is made of a relation name R and list of attributes A1, A2,......., An ) • The degree (or arity ) of a relation is the number of attributes n of its relation schema. • For example: the following relation schema represents a relation of degree seven and describes university students, student(Name, Ssn, Home_phone,Address, Office_phone, Age, Gpa) 8
  • 9. Tuple • A relation r of relation schema R(A1, A2 , A3, ..., An ), also denoted by r(R), is a set of n-tuples, r = { t1 , t2 , t3 ,... , tn}. • Each n-tuple t is an ordered list of n values t = < t1 , t2 , t3 ,... , tn >. • The ith value of n-tuple t corresponds to the attribute Ai . • The terms relation intension for schema R and relation extension for a relation state r(R) are commonly used. 9
  • 10. 10
  • 11. Relational Model Constraints  There are generally many restrictions or constraints on the actual values in a database state.  These constraints are derived from the rules in the miniworld that the database represents.  Constraints on the database can generally be divided into three main categories.  Inherent model-based or implicit constraints: For example: the relation cannot have duplicate tuples is an inherent constraint.  Schema based constraints or explicit constraints: These constraints are expressed in schema of relational model via the DDL. For example: domain constraints and Keys  Application based or semantic constraints or business rules: functional dependency or multivalued dependencies. 11
  • 12. Domain Constraints  Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value from the domain dom(A).  This is specified as data types which includes standard numeric data types integers, real numbers, characters, Booleans, fixed length strings, variable length strings etc.  These are specified in DDL statements. 12
  • 13. KEY Constraints Definition ◦ A Column value in the table that uniquely identifies a single record in the table is called key of a table. ◦ An attribute or the set of attribute in the table that uniquely identifies each record in the entity set is called a key for that entity set Types of keys ◦ Simple Key: A key which has the single attribute is known as a simple key. ◦ Composite key: A key which consist two or more attributes is called a Composite Key. Example: Cust_id is a key attribute of Customer Table it is possible to have a single key for one customer i.e is Cust_id ie Cust_id =1 is only for the Cust_name =”Yogesh”.Please refer to the Customer Table which is mentioned below. 13
  • 14. Types of keys Types of Key Definition of Key Super Key A key is called super key which is sufficient to identify the unique record in the table Candidate Key A minimal super key is called candidate key. A super key that has no proper subset of candidate key. Primary Key A candidate key is chosen as a principal to identify a unique. Foreign Key An column ( or combination of columns) in the one table whose values is match the primary key in the another table 14
  • 15. Customer table Cust_id Cust_Name Cust_Age Cust_Address Cust_Mobile_N o Cust_Phone_No 1 Yogesh 20 Kathmandu 9841567081 4434999 2 Ramesh 23 Pokhara 9985555522 4443434 3 Ram 18 Chitwan 9854333332 656565 4 Pramod 24 Dailekh 9848044401 34343434 5 Yashu 25 Birendranagar 9848080000 44545454 6 Sarawsati 23 Gulmi 9855555555 93434343 15
  • 16. Super Key and key  Any set of attributes SK is called superkey of the relation schema R, if for any two distinct tuples t1 and t2 in the relation state r of R, we have the constraint that t1[SK] ≠ t2[SK].  A key k of a relation schema R is a superkey of R with the additional property that removing any attribute A from K leaves a set of attributes K’ that is not a superkey of R any more.  Hence a key satisfies two constraints: 1. Two distinct tuples in any state of the relation cannot have identical values for all the attributes in the key. 2. It is a minimal superkey – that i, a superkey from which we cannot remove any attributes and still have the uniqueness constraints in condition 1 hold. 16
  • 17. Super key and key  Example:  Cust_id attribute of the entity set Customer uniquely identifies Customer entity from another, so the Cust_id is the Super key.  Another way is, the combination of Cust_id attribute and Cust_Name attribute is the Super key for the Customer Entity set.  Only the Cust_Name is not called the Super Key because several customer may have the Same Name  Cust_id is called key as it is minimal super key. 17
  • 18. Candidate Keys  In general a relation schema may have more than one key.  In this case, each of them is called a candidate key.  For example, the CAR relation has two candidate keys: License_Number and Engine_serial_Number.  It is common to designate one of the candidate keys as the primary key.  This is the Candidate key whose values are used to identify tuples in a relation. License_Nu mber Engine_serial_Numbe r Make Model Year 18
  • 19. NOT NULL and Entity Integrity Constraints  NOT-NULL constraint: It is a constraint on attributes whether null values are or are not permitted. For example, if every Customer tuple must be valid, non-null value for the cust_Name attribute, then cust_Name of customer is constrained to be NOT NULL.  Entity Integrity Constraint: It states that no primary key value can be null. This is because the primary key value is used to identify individual tuples in a relation.  Key constraints and entity integrity constraints are specified on individual relations. 19
  • 20. Referential Integrity Constraint  The referential Integrity Constraints are specified between two relations and is used to maintain the consistency among tuples in the two relations.  It states that a tuple in one relation that refers to another relation must refer to an existing tuple in that relation.  To define the referential integrity more formally, the concept of foreign key is needed.  All the integrity constraints should be specified on the relational database schema if we want to enforce these constraints on the database state.  A database state that does not obey all the integrity constraints is called an invalid state. 20
  • 21. Foreign Key  A set of attributes FK is relation schema R1 is a foreign key of R1 that references relation R2 if it satisfies the following rules:  The attributes in FK have the same domain(s) as the primary key attributes PK or R2  A value of FK in a tuple t1 of the current stare r1(R1) either occurs as a value of PK for some tuple t2 in the current state r2(R2) or is NULL. i.e. t1[FK] = t2[PK] and we say that the tuple t1 references or refers to the tuple t2.  In this definition R1 is referencing relation and R2 is referenced relation.  If these two conditions hold, a referential integrity constraint from R1 to R2 is said to hold. 21
  • 22. Example: Referential Integrity ( Company database) 22
  • 23. 23
  • 24. Example: Referential Integrity  In the EMPLOYEE relation, the attribute Dno refers to the department for which an employee works; hence, we designate Dno to be a foreign key of EMPLOYEE referencing the DEPARTMENT relation.  This means that a value of Dno in any tuple t1 of the EMPLOYEE relation must match a value of the primary key of DEPARTMENT—the Dnumber attribute—in some tuple t2 of the DEPARTMENT relation, or the value of Dno can be NULL if the employee does not belong to a department or will be assigned to a department later.  For example, in above tables, the tuple for employee ‘John Smith’ references the tuple for the ‘Research’ department, indicating that ‘John Smith’ works for this department. 24
  • 25. Other Constraints 1. Semantic integrity constraints:  These are constraints specified and enforced within the application programs that update the database, or by using general purpose constraint specification language.  For example: salary of an employee should not exceed the salary of the employee’s supervisor and maximum number of hours an employee can work on all projects per week is 56.  Mechanisms called triggers and assertions can be used. 2. Functional dependency constraint:  It establishes a functional relationship among two sets of attributes X and Y.  This constraint specifies that the value of X determines the value of Y in all states of a relation; it is denoted as a functional dependency X -> Y. 25
  • 26. Database Operations on relational Model  The operations of the relational model can be categorized into retrievals and updates.  The relational algebra operations can be used to specify retrievals.  A relational algebra expression forms a new relation after applying a number of algebraic operators to an existing set of relations; its main use is for querying a database.  This relation becomes the answer to the user’s query.  The relational calculus also defines new relation declaratively without giving a specific order of operations. 26
  • 27. Update operations  There are three basic update operations on relations: insert, delete and modify.  insert is used to insert a new tuple or tuples in a relation.  Delete is used to delete tuples.  Update or Modify is used to change the values of some attributes in existing tuples.  Whenever these operations are applied, integrity constraints specified on the relational database schema should not be violated. 27
  • 28. Insert Operation  The insert operation provides a list of attribute values for a new tuple t that is to be inserted into a relation R.  Insert can violate any of the four constraints discussed above.  Domain constraints can be violated if an attribute value is given that does not appear in the corresponding domain or is not of the appropriate data type.  Key constraints can be violated if a key value in the new tuple t already exists in another tuple in the relation r(R).  Entity integrity can be violated if any part of the primary key of the new tuple t is NULL.  Referential integrity can be violated if the value of any foreign key in t refers to a tuple that does not exist in the referenced relation. 28
  • 29. Insert operation  An unary operation.  Insert a new tuple into the relation. 29
  • 30. The Delete Operation  The Delete operation can violate only referential integrity.  This occurs if the tuple being deleted is referenced by foreign keys from other tuples in the database.  To specify deletion, a condition on the attributes of the relation selects the tuple (or tuples) to be deleted.  In general, when a referential integrity constraint is specified in the DDL, the DBMS will allow the database designer to specify which of the options applies in case of a violation of the constraint. 30
  • 31. The Delete operation  An unary operation.  Delete a tuple defined by a criterion from the relation. 31
  • 32. The Update Operation  The Update (or Modify) operation is used to change the values of one or more attributes in a tuple (or tuples) of some relation R.  It is necessary to specify a condition on the attributes of the relation to select the tuple (or tuples) to be modified.  Updating an attribute that is neither part of a primary key nor of a foreign key usually causes no problems; the DBMS need only check to confirm that the new value is of the correct data type and domain.  Modifying a primary key value is similar to deleting one tuple and inserting another in its place because we use the primary key to identify tuples. 32
  • 33. The Update operation  An unary operation.  Changes the value of some attributes of a tuple. 33
  • 34. Relational Algebra and Relational Calculus  These are the two formal languages for relational model.  A data model must include a set of operations to manipulate the database, in addition to the data model’s concepts for defining database structures and constraints.  The basic set of operations for the relational model is the relational algebra. These operations enable a user to specify basic retrieval requests. The result of a retrieval is a new relation, which may have been formed from one or more relations, which can be further manipulated using operations of same relational algebra.  The relation algebra is very important for serval reasons.  First, it provides a formal foundation for relational model operations.  Second, it is used as a basis for implementing and optimizing queries in RDBMS.  Third, the core operations and functions of any relational systems are based on relational algebra operations. 34
  • 35. Relational Algebra and Relational Calculus  Relational calculus provides a higher-level declarative notation for specifying relational queries.  In a calculus expression, there is no order of operations to specify how to retrieve the query result – a calculus expression specifies only what information the result should contain.  Relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database ( in tuple relational calculus) over columns of the stored relations ( in domain calculus).  The relational calculus is important because it has firm basis in mathematical logic and the standard query language (SQL) has some of its foundation in tuple relational calculus. 35
  • 36. Relational Algebra  The relational algebra is often considered to be an integral part of the relational data model.  Its operations can be divided into two groups.  One group includes set operations from mathematical set theory; these are applicable because each relation is defined to be a set of tuples in the formal relational model. Set operations include UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT (also known as CROSS PRODUCT).  The other group consists of operations developed specifically for relational databases—these include SELECT, PROJECT, and JOIN, among others. 36
  • 37. The select operation  The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a selection condition.  One can consider the SELECT operation to be a filter that keeps only those tuples that satisfy a qualifying condition.  For example, to select the EMPLOYEE tuples whose department is 4, or those whose salary is greater than $30,000, we can individually specify each of these two conditions with a SELECT operation as follows: σDno=4(EMPLOYEE) σSalary>30000(EMPLOYEE) 37
  • 38. The select Operation  In general, the SELECT operation is denoted by σ<selection condition> (R)  where the symbol σ (sigma) is used to denote the SELECT operator and the selection condition is a Boolean expression (condition) specified on the attributes of relation R.  Notice that R is generally a relational algebra expression whose result is a relation—the simplest such expression is just the name of a database relation.  The relation resulting from the SELECT operation has the same attributes as R.  For example, to select the tuples for all employees who either work in department 4 and make over $25,000 per year, or work in department 5 and make over $30,000, we can specify the following SELECT operation: σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE) 38
  • 39. The Select operation  An unary operation.  It is applied to one single relation and creates another relation.  The tuples in the resulting relation are a subset of the tuples in the original relation.  Use some criteria to select 39
  • 40. The Project Operation  The PROJECT operation selects certain columns from the table and discards the other columns.  If we are interested in only certain attributes of a relation, we use the PROJECT operation to project the relation over these attributes only.  Therefore, the result of the PROJECT operation can be visualized as a vertical partition of the relation into two relations:  One has the needed columns (attributes) and contains the result of the operation, and the other contains the discarded columns.  For example, to list each employee’s first and last name and salary, we can use the PROJECT operation as follows: πLname, Fname, Salary(EMPLOYEE) 40
  • 41. The Project Operation  The general form of the PROJECT operation is π<attribute list> (R)  where π (pi) is the symbol used to represent the PROJECT operation, and < attribute list > is the desired sub list of attributes from the attributes of relation R.  Again, notice that R is, in general, a relational algebra expression whose result is a relation, which in the simplest case is just the name of a database relation.  The result of the PROJECT operation has only the attributes specified in <attribute list> in the same order as they appear in the list.  Hence, its degree is equal to the number of attributes in <attribute list>. 41
  • 42. The Project Operation  An unary operation.  It is applied to one single relation and creates another relation.  The attributes in the resulting relation are a subset of the attributes in the original relation. 42
  • 43. Rename Operation  In general, for most queries, we need to apply several relational algebra operations one after the other. Either we can write the operations as a single relational algebra expression by nesting the operations, or we can apply one operation at a time and create intermediate result relations.  In the latter case, we must give names to the relations that hold the intermediate results.  For example, to retrieve the first name, last name, and salary of all employees who work in department number 5, we must apply a SELECT and a PROJECT operation.  We can write a single relational algebra expression, also known as an in-line expression, as follows: πFname, Lname, Salary (σDno=5(EMPLOYEE))  Alternatively, we can explicitly show the sequence of operations, giving a name to each intermediate relation, as follows:  DEP5_EMPS ← σDno=5(EMPLOYEE)  RESULT ← πFname, Lname, Salary(DEP5_EMPS) 43
  • 44. Relational Algebra Operations from Set Theory  Several set theoretic operations are used to merge the elements of two sets in various ways, including UNION, INTERSECTION, and SET DIFFERENCE (also called MINUS or EXCEPT).  These are binary operations; that is, each is applied to two sets (of tuples).  When these operations are adapted to relational databases, the two relations on which any of these three operations are applied must have the same type of tuples; this condition has been called union compatibility or type compatibility.  Two relations R(A1, A2, ..., An) and S(B1, B2, ..., Bn) are said to be union compatible (or type compatible) if they have the same degree n and if dom(Ai) = dom(Bi) for 1 <=i<= n.  This means that the two relations have the same number of attributes and each corresponding pair of attributes has the same domain. 44
  • 45. Relational Algebra Operations from Set Theory  We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE on two union-compatible relations R and S as follows:  UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated.  INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S.  SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that includes all tuples that are in R but not in S. 45
  • 46. Example  For example, to retrieve the Social Security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5, we can use the UNION operation as follows:  DEP5_EMPS ← σDno=5(EMPLOYEE)  RESULT1 ← πSsn(DEP5_EMPS)  RESULT2 ← πSuper_ssn(DEP5_EMPS)  RESULT ← RESULT1 ∪ RESULT2 46
  • 47. Union operation  A binary operation.  Creates a new relation in which each tuple is either in the first relation, in the second, or in both.  The two relations must have the same attributes. 47
  • 48. Intersection operation  A binary operation.  Creates a new relation in which each tuple is a member in both relations.  The two relations must have the same attributes. 48
  • 49. The Difference operation  A binary operation.  Creates a new relation in which each tuple is in the first relation but not the second.  The two relations must have the same attributes. 49
  • 50. Cartesian Product  This is also a binary set operation, but the relations on which it is applied do not have to be union compatible.  In its binary form, this set operation produces a new element by combining every member (tuple) from one relation (set) with every member (tuple) from the other relation (set).  In general, the result of R(A1, A2, ..., An) × S(B1, B2, ..., Bm) is a relation Q with degree n + m attributes Q(A1, A2, ..., An, B1, B2, ..., Bm), in that order.  The resulting relation Q has one tuple for each combination of tuples—one from R and one from S.  Hence, if R has nR tuples (denoted as |R| = nR), and S has nS tuples, then R × S will have nR * nS tuples. 50
  • 51. The Join Operation  The JOIN operation, denoted by , is used to combine related tuples from two relations into single “longer” tuples.  The JOIN operation can be specified as a CARTESIAN PRODUCT operation followed by a SELECT operation.  Suppose that we want to retrieve the name of the manager of each department. To get the manager’s name, we need to combine each department tuple with the employee tuple whose Ssn value matches the Mgr_ssn value in the department tuple. • Note that Mgr_ssn is a foreign key of the DEPARTMENT relation that references Ssn, the primary key of the EMPLOYEE relation. This referential integrity constraint plays a role in having matching tuples in the referenced relation EMPLOYEE. 51
  • 52. The join operation  The general form of a JOIN operation on two relations R(A1, A2, ..., An) and S(B1, B2, ..., Bm) is R ⋈ <join condition>S  The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, ..., An, B1, B2, ... , Bm) in that order; Q has one tuple for each combination of tuples—one from R and one from S—whenever the combination satisfies the join condition.  This is the main difference between CARTESIAN PRODUCT and JOIN. In JOIN, only combinations of tuples satisfying the join condition appear in the result, whereas in the CARTESIAN PRODUCT all combinations of tuples are included in the result.  The join condition is specified on attributes from the two relations R and S and is evaluated for each combination of tuples.  Each tuple combination for which the join condition evaluate to TRUE is included in the resulting relation Q as a single combined tuple. 52
  • 53. The Join operation  A binary operation.  Combines two relations based on common attributes. 53
  • 54. Variations of Join  Theta Join: produces all combinations of tuples form R1 and R2 that satisfy the join condition  Equijoin: Produces all the combinations of tuples from R1 and R2 that satisfy a join condition with only equality comparisons.  Natural Join: Same as equijoin except that the join attributes of R2 are not included in the resulting relation; if the join attributes have the same names, they do not need to be specified at all.  In general, NATURAL JOIN is performed by equating all the attribute pairs that have the same name in the two relations. 54
  • 55. Example:  The join of two relations creates a new relation. The notation R1 x R2 indicates the join of relations R1 and R2. For example, consider the following: Relation R1 A B C 1 5 3 2 4 5 8 3 5 9 3 3 1 6 5 5 4 3 2 7 5 Relation R2 B D E 4 7 4 6 2 3 5 7 8 7 2 3 3 2 2 Relation R1 x R2 A B C D E 1 5 3 7 8 2 4 5 7 4 8 3 5 2 2 9 3 3 2 2 1 6 5 2 3 5 4 3 7 4 2 7 5 2 3 The row (2 4 5 7 4) was formed by joining the row (2 4 5) from relation R1 to the row (4 7 4) from relation R2. The two rows were joined since each contained the same value for the common attribute B. The row (2 4 5) was not joined to the row (6 2 3) since the values of the common attribute (4 and 6) are not the same. 55
  • 56. Inner and Outer Joins  INNER joins contain only matches.  OUTER joins may contain mismatches as well.  Inner Join:- This is sometimes known as a simple join. It returns all rows from both tables where there is a match. If there are rows in R1 which do not have matches in R2, those rows will not be listed.  Left Outer Join:- Returns all the rows from R1 even if there are no matches in R2. If there are no matches in R2 then the R2 values will be shown as null.  Right Outer Join:- Returns all the rows from R2 even if there are no matches in R1. If there are no matches in R1 then the R1 values will be shown as null.  Full Outer Join:- Returns all the rows from both tables even if there are no matches in one of the tables. If there are no matches in one of the tables then its values will be shown as null. 56