DBMS Unit 2 Notes
DBMS Unit 2 Notes
4 SURESH DELHI 18
Database consists of various components
1) Tables − In relational data model, relations are saved in the format of Tables. This format stores the
relation among entities. A table has rows and columns, where rows represents records and columns
represent the attributes.
2) Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
3) Relation instance − A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.Table 1 shows the relation instance of
STUDENT at a particular time.
4) Relation schema − A relation schema describes the relation name (table name), attributes, and their
names.
5) Relation key − Each row has one or more attributes, known as relation key, which can identify the
row in the relation (table) uniquely.
6) Attribute - Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc.
7) Column - The column represents the set of values for a specific attribute.
8) Degree - The total number of attributes which in the relation is called the degree of the
relation.The STUDENT relation defined above has degree 5.
9) Cardinality - Total number of rows present in the Table.The STUDENT relation defined above has
cardinality 4.
10) NULL Values: The value which is not known or unavailable is called a NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
Constraints in Relational Model
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The value of
the attribute must be available in the corresponding domain.
Example:
2. Entity integrity constraints
Entity integrity constraint specifies that no attribute of primary key must contain a null value in any
relation.
This is because the presence of null value in the primary key violates the uniqueness property.
Example:
Example:
4). Key constraints
An entity set can have multiple keys, but out of which one key will be the primary key. Example:Play
Video
S002 Abhishek 21
S003 Shashank 20
S004 Rahul 20
This relation satisfies the tuple uniqueness constraint since here all the tuples are unique.
Example-02:
Consider the following Student table-
S001 Akshay 20
S001 Akshay 20
S003 Shashank 20
S004 Rahul 20
This relation does not satisfy the tuple uniqueness constraint since here all the tuples are not unique.
KEYS
KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a
relation(table).
Key is also helpful for finding unique record or row from the table.
Types of Keys
1. Super Key
2. Candidate Key
3. Primary Key
4. Foreign Key
5. Composite Key
6. Alternate Key
7. Unique Key
1)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.
Example-
Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )
Given below are the examples of super keys since each set can uniquely identify each student in the
Student table-
( roll , name , sex , age , address , class , section )
( class , section , roll )
(class , section , roll , sex )
( name , address )
2)Candidate Key
The value of candidate key must always be unique.
The value of candidate key can never be NULL.
It is possible to have multiple candidate keys in a relation.
A set of minimal attribute(s) that can identify each tuple uniquely in the given relation is called as a
candidate key.
Example-
Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )
Given below are the examples of candidate keys since each set consists of minimal attributes required to
identify each student uniquely in the Student table-
( class , section , roll )
( name , address )
3) Primary key :-A primary key is a candidate key that the database designer selects while designing the
database.
The value of primary key can never be NULL.
The value of primary key must always be unique.
The values of primary key can never be changed i.e. no updation is possible.
The value of primary key must be assigned when inserting a record.
A relation is allowed to have only one primary key.
4)Alternate Key
As we have stated above, any table can consist of multiple choices for the primary key. But, it can only
choose one. Thus, all those keys that did not become a primary key are known as alternate keys.
5) Foreign Key-
An attribute ‘X’ is called as a foreign key to some other attribute ‘Y’ when its values are dependent on the
values of attribute ‘Y’.
Example-
Consider the following two schemas-
Here, t_dept can take only those values which are present in dept_no in Department table since only those
departments actually exist.
Foreign key references the primary key of the table.
Foreign key can take only those values which are present in the primary key of the referenced relation.
Foreign key may have a name other than that of a primary key.
Foreign key can take the NULL value.
There is no restriction on a foreign key to be unique.
In fact, foreign key is not unique most of the time.
Referenced relation may also be called as the master table or primary table.
Referencing relation may also be called as the foreign table.
6) Composite Key
A composite Key is a set of two or more attributes that help identify each tuple in a table uniquely. The
attributes in the set may not be unique when considered separately. However, when taken all together, they
will ensure uniqueness. The ‘concatenated key’ is another name for a composite key.
7) Unique Key-
Unique key is a key with the following properties-
It is unique for all the records of the table.
Once assigned, its value can not be changed i.e. it is non-updatable.
It may have a NULL value.
Example-The best example of unique key is Adhaar Card Numbers.
The Adhaar Card Number is unique for all the citizens (tuples) of India (table).
If it gets lost and another duplicate copy is issued, then the duplicate copy always has the same number as
before.
Thus, it is non-updatable.
Few citizens may not have got their Adhaar cards, so for them its value is NULL.
Objective Relational Algebra targets how to Relational Calculus targets what result
2
obtain the result. to obtain.
Procedure Relational Algebra means how to Relational Calculus means what result
3
obtain the result. we have to obtain.
Order Relational Algebra specifies the order Relational Calculus specifies no such
4 in which operations are to be order of executions for its operations.
performed.
Dependency Relational Algebra is domain Relational Calculus can be domain
5 independent. dependent.
Database For accessing the database, relational For accessing the database, relational
access algebra provides a solution in terms calculus provides a solution in terms
7 of what is required and how to get as simple as what is required and lets
that information by following a step- the system find the solution for that.
by-step description.
RELATIONAL ALGEBRA
Relational Algebra is a procedural language. In Relational Algebra, The order is specified in which the
operations have to be performed. In Relational Algebra, frameworks are created to implement the queries.
The basic operation included in relational algebra is:
1. Select (σ)
2. Project (Π)
3. Union (U)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Additional operations
7)Join
8)Intersection
9)Divide
10)Assignment
1) SELECT (σ)
Select operation is done by Selection Operator which is represented by "sigma"(σ). It is used to retrieve
tuples (rows) from the table where the given condition is satisfied. It is a unary operator means it requires
only one operand.
It selects tuples from a relation that satisfy the provided predicate.
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
Example1:
Find the customer details who are living in Hyderabad city and whose customer_id is greater than
1000 in Customer relation.
A B C D
α α 1 7
α β 5 7
β β 12 3
β β 23 10
σA=B∧D>5(r)
A B C D
α α 1 7
β β 23 10
2) Project (∏)
Project operation is done by Projection Operator which is represented by "pi"(∏). It is used to retrieve
certain attributes(columns) from the table. It is also known as vertical partitioning as it separates the table
vertically. It is also a unary operator.
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list
r πA,C(r)
A B C A C
α 10 2 α 2
α 20 2 β 2
β 30 2 β 4
β 40 4
Example2: Find the customer names (not all customer details) who are living in Hyderabad city
from customer relation.
In the above example, the selection operation is performed first. Next, the projection of
the resulting relation on the customer name column is carried out. Thus, instead of all customer
details of customers living in Hyderabad city, we can display only the customer names of
customers living in Hyderabad city.
The above example is also known as relational algebra expression because we are
combining two or more relational algebra operations (ie., selection and projection) into one at
the sometime.
Example3: Find the customer names (not all customer details) from customer relation.
customer_name ( customer )
The above stated query lists all customer names in the customer relation and this is not
called as relational algebra expression because it is performing only one relational
algebra operation.
Note:- The Selection operation eliminates unwanted rows whereas the projection operation
eliminates unwanted columns.
3) Rename (ρ)
Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to rename the output relation.
Rename operator too is a binary operator.
Notation: ρ(R,S)
Where R is the new relation name
S is the old relation name
It helps in combining data and info of two differing relations into a single one.
Notation: R X S
Where R is the first relation
S is the second relation
r s
A B C D E
α 1 α 10 +
β 2 β 10 +
β 20 −
γ 10
−
r ×s
A B C D E
α 1 α 10 +
α 1 β 10 +
α 1 β 20 −
α 1 γ 10 −
β 2 α 10 +
β 2 β 10 +
β
2
β
20 −
β
2
γ
10 −
5) Union Operation (or ∪)
It includes all tuples that are in tables A or in B. It also eliminates duplicate tuples.
Example
Example
Consider two tables R1 and R2 −
Table R1 is as follows −
1 CSE A
2 ECE B
3 MECH B
4 CIVIL A
5 CSE B
Table R2 is as follows −
1 CIVIL A
2 CSE A
3 ECE B
To display all the regno of R1 and R2, use the following command −
∏regno(R1) ∪ ∏regno(R2)
Output
Regno
1
Regno
5
To retrieve branch and section of student from table R1 and R2, use the below mentioned
command −
∏ branch, section (R1) ∪ ∏ branch, section (R2)
Output
Branch Section
CSE A
ECE B
MECH B
CIVIL A
CSE B
No repletion allowed for entry.
Union All − It is used for duplicate entries. The common entries will be shown if the common
intersection is used.
Tuples refers to the result of the set difference operation. These are present in just one of the
relations but not at all in the second one.
r s
A B A B
α 1 α 2
α 2 β 3
β 1
r −s
A B
α 1
β 1
Additional Operations
7) Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and B. However, A and B
must be union-compatible.
Visual
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
8)Division
The division operator is used for queries which involve the ‘all’.
R1 ÷ R2 = tuples of R1 associated with all tuples of R2.
Figure 1 explains that when the division operation is performed, it will retrieve only those lecturer
from relation R who has taken a subject “Prolog” from relation S.