0% found this document useful (0 votes)
2 views

DBMS_ENDSEM

The document provides an overview of Database Management Systems (DBMS), explaining the concepts of data, databases, and the importance of DBMS in managing large information bodies. It discusses various database applications, the purpose of database systems, and the problems associated with traditional file systems. Additionally, it covers database models, languages, keys, and entity-relationship models, highlighting their significance in organizing and accessing data.

Uploaded by

horridbeast000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS_ENDSEM

The document provides an overview of Database Management Systems (DBMS), explaining the concepts of data, databases, and the importance of DBMS in managing large information bodies. It discusses various database applications, the purpose of database systems, and the problems associated with traditional file systems. Additionally, it covers database models, languages, keys, and entity-relationship models, highlighting their significance in organizing and accessing data.

Uploaded by

horridbeast000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 335

Database Management System

Presented By:-
Dr. Sanjeev Sharma
Introduction
• Data: Known facts that can be recorded and have an
implicit meaning.

• Database: A collection of related data.

• DBMS contains information about a particular


enterprise
– Collection of interrelated data
– Set of programs to access the data
– An environment that is both convenient and efficient to use
• Database systems are designed to manage large
bodies of information.
• Management of data involves both defining
structures for storage of information and
providing mechanisms for the manipulation of
information.
• In addition, the database system must ensure the
safety of the information stored, despite system
crashes or attempts at unauthorized access.
• If data are to be shared among several users, the
system must avoid possible anomalous results.
• Database Applications:
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Online retailers: order tracking, customized
recommendations
– Manufacturing: production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax
deductions
• Databases touch all aspects of our lives
Purpose of Database Systems
• In the early days, database applications were built
directly on top of file systems
• Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
• Multiple file formats, duplication of information in different files
– Difficulty in accessing data
• Need to write a new program to carry out each new task
– Data isolation —
– Integrity problems
• Integrity constraints (e.g. account balance > 0) become “buried” in
program code rather than being stated explicitly
• Hard to add new constraints or change existing ones
– Atomicity of updates
• Failures may leave database in an inconsistent state with
partial updates carried out
• Example: Transfer of funds from one account to another
should either complete or not happen at all
– Concurrent access by multiple users
• Concurrent accessed needed for performance
• Uncontrolled concurrent accesses can lead to inconsistencies
– Example: Two people reading a balance and updating it at the
same time
– Security problems
• Hard to provide user access to some, but not all, data
• Database systems offer solutions to all the above problems
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Manages Interaction Between end
Users and Database
Example of a Database
Mini-world for the example: Part of a UNIVERSITY environment.
Some mini-world entities (Data elements):
- STUDENTs
- COURSEs
- SECTIONs (of COURSEs)
- (academic) DEPARTMENTs
- INSTRUCTORs
Some mini-world relationships:
- SECTIONs are of specific COURSEs
- STUDENTs take SECTIONs
- COURSEs have prerequisite COURSEs
- INSTRUCTORs teach SECTIONs
- COURSEs are offered by DEPARTMENTs
- STUDENTs major in DEPARTMENTs
Schema and Instances
• Similar to types and variables in programming languages
• Schema – the logical structure of the database
– Example: The database consists of information about a set
of customers and accounts and the relationship between
them)
– Analogous to type information of a variable in a program
• Instance – the actual content of the database at a particular
point in time
– Analogous to the value of a variable
Data Abstraction in DBMS
• Database systems are made-up of complex data structures. To
ease the user interaction with database, the developers hide
internal irrelevant details from users.

• This process of hiding irrelevant details from user is called


data abstraction.
• Physical level: This is the lowest level of data abstraction. It
describes how data is actually stored in database. You can get
the complex data structure details at this level.

• Logical level: This is the middle level of 3-level data


abstraction architecture. It describes what data is stored in
database.

• View level: Highest level of data abstraction. This level


describes the user interaction with database system.
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Database Languages
• A DBMS must provide appropriate languages and interfaces
for each category of users to express database queries and
updates.

• Database Languages are used to create and maintain database


on computer.

• Database languages are used to read, update and store data in a


database. There are several such languages that can be used for
this purpose; one of them is SQL (Structured Query
Language).
DDL
• Data Definition Language (DDL) statements are used to
classify the database structure or schema. It is a type of
language that allows the DBA or user to depict and name those
entities, attributes, and relationships that are required for the
application along with any associated integrity and security
constraints. Here are the lists of tasks that come under DDL:
– CREATE - used to create objects in the database
– ALTER - used to alters the structure of the database
– DROP - used to delete objects from the database
– TRUNCATE - used to remove all records from a table, including all
spaces allocated for the records are removed
– COMMENT - used to add comments to the data dictionary
– RENAME - used to rename an object
DML
• A language that offers a set of operations to support the
fundamental data manipulation operations on the data held in
the database. Here are the lists of tasks that come under DML:
– SELECT - It retrieves data from a database
– INSERT - It inserts data into a table
– UPDATE - It updates existing data within a table
– DELETE - It deletes all records from a table, the space for
the records remain
– MERGE - UPSERT operation (insert or update)
DCL
• Grant privilege to a user using the GRANT statement. In the
same way, revoke the privilege using the REVOKE statement.
Both of these statements come under the Data Control
Language (DCL).:

• GRANT: Give privilege to access the database.

• REVOKE: Take back the privilege to access the database.


TCL
• Manage transactions in the Database using the Transaction
Control Language:
– COMMIT: Save the work.
– SAVEPOINT: Set a point in transaction to rollback later
– ROLLBACK: Restores since last commit
Database Models in DBMS
• A data model is an idea which describes how the data can be
represented and accessed from software system after its
complete implementation.
• It is a simple abstraction of complex real world data gathering
environment.
• It defines data elements and relationships among various data
elements for a specified system.
• The main purpose of data model is to give an idea that how
final system or software will look like after development is
completed.
Various Types of Data Models
• E-R model
• Relational Model
• Hierarchical Model
• Network Model
• Object Based Model
Entity Relationship Model
• Entity relationship model is based on the notion of the real
world entities and their relationships. This model focus on two
main things:
– Entity and their attributes
– Relationships among entities
• An Entity is a thing that can be distinguishable from others.
• An entity has a real world property called attribute and
attribute define by a set of values called domain.
• For example, in a university a student is an entity, university is
the database, name and age and sex are the attributes. The
relationships among entities define the logical association
between entities.
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Database Models in DBMS
• A data model is an idea which describes how the data can be
represented and accessed from software system after its
complete implementation.
• It is a simple abstraction of complex real world data gathering
environment.
• It defines data elements and relationships among various data
elements for a specified system.
• The main purpose of data model is to give an idea that how
final system or software will look like after development is
completed.
Various Types of Data Models
• E-R model
• Relational Model
• Hierarchical Model
• Network Model
• Object Based Model
Entity Relationship Model
• Entity relationship model is based on the notion of the real
world entities and their relationships. This model focus on two
main things:
– Entity and their attributes
– Relationships among entities
• An Entity is a thing that can be distinguishable from others.
• An entity has a real world property called attribute and
attribute define by a set of values called domain.
• For example, in a university a student is an entity, university is
the database, name and age and sex are the attributes. The
relationships among entities define the logical association
between entities.
Relational Model
• This model was introduced by E.F Codd in 1970, and
since then it has been the most widely used database
model.
– In this model, data is organised in two-dimensional tables and
the relationship is maintained by storing a common field.
– The basic structure of data in the relational model is tables. All
the information related to a particular type is stored in rows of
that table.
– Hence, tables are also known as relations in relational model.
Hierarchical Model
• This database model organises data into a tree-like-structure,
with a single root, to which all the other data is linked. The
hierarchy starts from the Root data, and expands like a tree,
adding child nodes to the parent nodes.

– In this model, a child node will only have a single parent node.
– Hierarchical model was developed by IBM and North American
Rockwell known as Information Management System.
– It represents the data in a hierarchical tree structure.
– In this model, the data is sorted hierarchically.
– It uses pointer to navigate between the stored data.
Network Data Model
• Network model has the entities which are organized in a
graphical representation and some entities in the graph
can be accessed through several paths.
– Network Database Model is same like Hierarchical Model, but
the only difference is that it allows a record to have more than
one parent.
– In this model, there is no need of parent to child association like
the hierarchical model.
– It replaces the hierarchical tree with a graph.
– It represents the data as record types and one-to-many
relationship.
– This model is easy to design and understand.
Object Model
• Object model stores the data in the form of objects, classes and
inheritance.
• This model handles more complex applications, such as
Geographic Information System (GIS), scientific experiments,
engineering design and manufacturing.
– It is used in File Management System.
– It represents real world objects, attributes and behaviors.
– It provides a clear modular structure.
– It is easy to maintain and modify the existing code.
Attributes
• An entity is represented by a set of attributes. Attributes
are descriptive properties possessed by each member of
an entity set.
– Customer is an entity and its attributes are customerid,
custmername, custaddress etc.

• An attribute as used in the E-R model , can be characterized by


the following attribute types.
– Simple and Composite Attributes
– Single-valued and Multi-valued Attribute
– Derived Attribute
– Null Valued Attribute
• Simple and composite attribute:
– simple attributes are the attributes which can’t be divided into
sub parts
eg: customerid, empno
– composite attributes are the attributes which can be divided into
subparts.
eg: name consisting of first name, middle name, last name
address consisting of city, pincode, state

• single-valued and multi-valued attribute:


– The attribute having single value is single –valued attribute
eg: empno,customerid,regdno etc.
The attribute having more than one value is multi-valued attribute
eg: phone-no, dependent name, vehicle
• Derived Attribute:
– The values for this type of attribute can be derived
from the values of existing attributes
eg: age which can be derived from (currentdate-
birthdate)
experience_in_year can be calculated as (currentdate-joindate)

• NULL valued attribute:


– The attribute value which is unknown to user is called
NULL valued attribute.
Keys
• A key is an attribute or set of attributes that is used to identify
data in entity sets. The attributes which are used as key are
known as key attributes. Rest of all are known as Non-key
attributes.
• Types of the Keys
– Super Keys
– Candidate Keys
– Primary Keys
– Alternative Keys
– Foreign Keys
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Keys
• A key is an attribute or set of attributes that is used to identify
data in entity sets. The attributes which are used as key are
known as key attributes. Rest of all are known as Non-key
attributes.
• Types of the Keys
– Super Keys
– Candidate Keys
– Primary Keys
– Alternative Keys
– Foreign Keys
• Super Key : A super key is a set of collection of one or more
than one attributes that can identify data uniquely.
• Any entity set has more than one super key.

• Ex. In entity set Employee (Reg. No, ID, Name, Salary,


DeptID) Super Keys are
– (a) (ID, Name, Salary, Reg. No.)
– (b) (ID, Name, Reg. No.)
– (c) (ID) etc.
• All combinations can identify data uniquely.
• Candidate Key : The minimal super key is known as
candidate key. Consider a super
• key and then take all of its proper subsets. If no one of the
proper subsets are super key.
• Then this super key is taken as candidate key.
• Ex. ID and Reg. No. are candidate key
• Primary Key : An attribute which identifies data uniquely is
known as Primary Key.
OR
• The term Primary Key is used to denote Candidate key.
• Any entity set can have more than one Candidate key but only
one Primary Key.
– Ex. In entity set Employee, either Reg. No. is primary key or ID
is primary key.
• Alternate Keys : All the candidate keys other than Primary
Key are known as Alternate Keys.
• Ex. If you take ID as Primary Key. Then, Reg. No. is an
alternate key.
• Foreign Key : A foreign key is an attribute in any entity
set which is also a Primary Key in any other entity set.
– Ex. Dept_ID: This is an attribute in entity set Employee and also a primary key
in entity
• set Department. Thus, it is a foreign key in Employee.
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Types of Entity Set
• There are two types of entity sets as given below:
• Strong Entity Sets
– Entity set having any key attributes are known as Strong Entity
sets.
– Ex. The Figure shows strong entity set Student having key
attribute Reg_No.
• Weak Entity Set
• Entity sets having no key attributes are known as Weak Entity
sets.
– Ex. The Figure shows weak entity set Table having no key attributes to
identify the tuples uniquely.

– A weak entity set is always associated with another entity set to make it
meaningful.
• This another entity set is known as Identifying entity set.
• E-R diagrams represents the logical structure of a database.
Symbols used in E-R diagrams are shown in Table.
Example E-R Diagram
Exa
[ 1'Js!ke BmL iE-R dlii8£F aDL havmg two entiity ~b," Cm1il1Dm.e:F 8ID1!1 Irt:eDil.,
Relationship Sets
• Relationship : A relationship is the association among several
entities. It connects different entities through a meaningful
relation.
• Relationship Set : A relationship set is a set of relationships
of the same type.
• Consider an example, employees work in different
departments. Then relationship exists between employees and
departments because each employee must belongs to some
department. Relation of all employees with department when
combined makes the relationship set because each employee
has same kind of relation with departments.
• Here, Employee and Department are two entity sets. r stands
for relationship between Employee and Department. Works_in
is the relationship set as shown in Figure.

• Descriptive Attributes : Attributes of any relationship set are


known as descriptive attributes.
Relationship Degree
• Relationship degree refers to the number of associated entities.
The relationship degree can be broadly classified into unary,
binary, and ternary relationship.
• Unary Relationship
• The unary relationship is otherwise known as recursive
relationship. In the unary relationship the number of associated
entity is one. An entity related to itself is known as recursive
relationship.
Binary Relationship
• In a binary relationship, two entities are involved. Consider the
example; each staff will be assigned to a particular department.
Here the two entities are STAFF and DEPARTMENT.
Ternary Relationship
• In a ternary relationship, three entities are simultaneously
involved. Ternary relationships are required when binary
relationships are not sufficient to accurately describe the
semantics of an association among three entities.
• Consider the example of employee assigned a project. Here we
are considering three entities EMPLOYEE, PROJECT, and
LOCATION. The relationship is “assigned-to.” Many
employees will be assigned to one project hence it is an
example of one-to-many relationship.
Thank You
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Constraints
• An E-R enterprise scheme may define certain constraints to
which the contents of a database must confirm.
• Mapping cardinalities or cardinality ratios, express the number
of entitities to which another entity can be associated via a
relationship set mapping cardinalities are most useful in
describing binary relationship sets.
• For a binary relationship set R between entity set A and B the
mapping cardinality must be one of the following :
– One to One
– One to Many
– Many to One
– Many to Many
One to one
• One to One (1 : 1) : An entity in X is associated with at most
one entity in Y and an entity in Y is associated with at most
one entity in X.
• A country has only one president. Any person may be the
president of at most one country.
One to Many (1 : N)
• An entity in X is associated with any number of entities in Y.
An entity in Y is associated with at most one entity in X.
• A manager has many employees under it but an employee
works under only one manager
Many to one
• Many to One (N : 1) : An entity in X is associated with at most
one entity in Y. An entity in Y is associated with any number
of entities in X.
• A employee can work on single project while any project can
be assigned to more than one employee.
• Many to Many (M : N) : An entity in X is associated with any
number (zero or more) of entities in Y and vice versa.
• A student can have more than one subject and one subject can
be given to more than one student.
Participation Constraints
• It tells the participation of entity set in relationship sets.
• There are two types of participations.
– Partial Participation
– Total Participation

• Partial Participation
• Partial participation : If only some entities from entity set E is
participated in relationships in set R then it is known as Partial
participation. Partial participation is shown in Figure (a).
• Total participation : If every entity from entity set E is
participated with at least one relation in relationship set R then
it is known as Total participation.

• Consider the Figure (b). Here Customer and Loan are two
entity sets and Relationship set is Borrower.
– Every customer may or may not take the Loan so Customer entity set is
partially participated.
– But every loan is concerned with at least one customer of bank. So
Loan entity set is totally participated.
Partial vs Total Participation
Reduction of E-R diagram to Table
• To represent the database in tabular form, E-R diagrams have
to be reduced in tables.
– Rule-01: For Strong Entity Set With Only Simple Attributes
– Rule-02: For Strong Entity Set With Composite Attributes-
– Rule-03: For Strong Entity Set With Multi Valued Attributes
– Rule-04: Translating Relationship Set into a Table-
– Rule-05: For Binary Relationships With Cardinality Ratios-
– Rule-06: For Binary Relationship With Both Cardinality
Constraints and Participation Constraints-
– Rule-07: For Binary Relationship With Weak Entity Set-
Rule-01
• For Strong Entity Set With Only Simple Attributes
• A strong entity set with only simple attributes will require only
one table in relational model.
– Attributes of the table will be the attributes of the entity set.
– The primary key of the table will be the key attribute of the entity set.
Rule -02
• For Strong Entity Set With Composite Attributes-
• A strong entity set with any number of composite attributes
will require only one table in relational model.
• While conversion, simple attributes of the composite attributes
are taken into account and not the composite attribute itself.
Rule-03
• For Strong Entity Set With Multi Valued Attributes
• A strong entity set with any number of multi valued attributes
will require two tables in relational model.
• One table will contain all the simple attributes with the
primary key.
• Other table will contain the primary key and all the multi
valued attributes.
Thank You
Database Management System

Presented By:-
Dr. Sanjeev Sharma
Reduction of E-R diagram to Table
• To represent the database in tabular form, E-R diagrams have
to be reduced in tables.
– Rule-01: For Strong Entity Set With Only Simple Attributes
– Rule-02: For Strong Entity Set With Composite Attributes-
– Rule-03: For Strong Entity Set With Multi Valued Attributes
– Rule-04: Translating Relationship Set into a Table-
– Rule-05: For Binary Relationships With Cardinality Ratios-
– Rule-06: For Binary Relationship With Both Cardinality
Constraints and Participation Constraints-
– Rule-07: For Binary Relationship With Weak Entity Set-
Rule-01
• For Strong Entity Set With Only Simple Attributes
• A strong entity set with only simple attributes will require only
one table in relational model.
– Attributes of the table will be the attributes of the entity set.
– The primary key of the table will be the key attribute of the entity set.
Rule -02
• For Strong Entity Set With Composite Attributes-
• A strong entity set with any number of composite attributes
will require only one table in relational model.
• While conversion, simple attributes of the composite attributes
are taken into account and not the composite attribute itself.
Rule-03
• For Strong Entity Set With Multi Valued Attributes
• A strong entity set with any number of multi valued attributes
will require two tables in relational model.
• One table will contain all the simple attributes with the
primary key.
• Other table will contain the primary key and all the multi
valued attributes.
Rule-04:
• Translating Relationship Set into a Table-
• A relationship set will require one table in the relational model.
• Attributes of the table are-
– Primary key attributes of the participating entity sets
– Its own descriptive attributes if any.
• Set of non-descriptive attributes will be the primary key.
Rule-5
• Rule-05: For Binary Relationships With Cardinality
Ratios-
– Case-01: Binary relationship with cardinality ratio m:n
– Case-02: Binary relationship with cardinality ratio 1:n
– Case-03: Binary relationship with cardinality ratio m:1
– Case-04: Binary relationship with cardinality ratio 1:1
• Case-01: For Binary Relationship With Cardinality Ratio
m:n
• Case-02: For Binary Relationship With
Cardinality Ratio 1:n
• Case-03: Binary relationship with cardinality ratio m:1
• Case-04: Binary relationship with cardinality ratio 1:1
Converting Ternary Relationship to
Tables
• The primary key attributes – Patient ID, Physician ID, and
Treatment Code – become foreign keys in PATIENT
TREATMENT. These attributes are components of the primary
key of PATIENT TREATMENT.
Thumb Rules to Remember
• While determining the minimum number of tables required
for binary relationships with given cardinality ratios,
following thumb rules must be kept in mind-
• For binary relationship with cardinality ration m : n ,
separate and individual tables will be drawn for each entity
set and relationship.
• For binary relationship with cardinality ratio either m : 1 or
1 : n , always remember “many side will consume the
relationship” i.e. a combined table will be drawn for many
side entity set and relationship set.
• For binary relationship with cardinality ratio 1 : 1 , two
tables will be required. You can combine the relationship set
with any one of the entity sets.
• Rule-06: For Binary Relationship With Both Cardinality
Constraints and Participation Constraints-
• Cardinality constraints will be implemented as discussed in
Rule-05.
• Because of the total participation constraint, foreign key
acquires NOT NULL constraint i.e. now foreign key can not
be null.
• Case-01: For Binary Relationship With Cardinality
Constraint and Total Participation Constraint From One
Side-
• Case-02: For Binary Relationship With Cardinality
Constraint and Total Participation Constraint From Both
Sides-
• If there is a key constraint from both the sides of an entity set
with total participation, then that binary relationship is
represented using only single table.
• Rule-07: For Binary Relationship With Weak Entity Set-
• Weak entity type does not have an independent existence and it
exists only through an identifying relationship with another
entity type called the owner.
• For each weak entity type, create a new relation and include all
of the simple attributes as attributes of the relation. Then
include the primary key of the identifying relation as a foreign
key attribute to this new relation.
• The primary key of the new relation is the combination of the
primary key of the identifying and the partial identifier of the
weak entity type. In this example DEPENDENT is weak
entity.
Thank You
Relational Model

Presented By:-
Dr. Sanjeev Sharma
Relational Model
• The relational model uses a collection of tables to represent
both data and the relationships among those data.

• A table is a collection of rows and columns. Each column has


a unique name.

• Each row in the table represents a collection of related data


values. In the relational model, a row is called a tuple, a
column header is called an attribute and the table is called a
relation.
Attribute and Domain
• Each attribute of a relation has a name
• The set of allowed values for each attribute is called the
domain of the attribute
• Attribute values are (normally) required to be atomic; that is,
indivisible
– E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
• Domain is said to be atomic if all its members are atomic
• The special value null is a member of every domain.
Relation Schema
• Formally, given domains D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn

Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai 


Di
• Schema of a relation consists of
– attribute definitions
• name
• type/domain
– integrity constraints
Database
• A database consists of multiple relations

• Information about an enterprise is broken up into parts, with


each relation storing one part of the information

• E.g.
account : information about accounts
depositor : which customer owns which account
customer : information about customers
Integrity Constraints
• These are the rules or constraints applied to the database to
keep data stable, accurate or consistent. To keep database
consistent we have to follow some rules known as integrity
rules or integrity constraints.
– Referential Integrity Rule
– Domain Constraints
– Key Constraints
Referential Integrity Rule
• A foreign key can be either null or it can have only those
values which are present in the primary key with which it is
related.

• Suppose A be the attribute in relation R1, which is also the


primary key in relation R2, then value of A in R1 is either null
or same as in relation R 2.
Domain Constraints
• The restrictions which we applied on domain are known as
domain constraints. These restrictions are applied to every
value of attribute. By following these constraints you can keep
consistency in database. These restrictions include data types
(integer, varchar, char, time format, date format etc.), size of
variable, checks (like value not null etc.) etc.
Key Constraints
• In any relation R, if attribute A is primary key then A must
have unique value or you can say that primary key attribute
must have unique value.
• Duplicate values in primary key are invalid.
Relational Model

Presented By:-
Dr. Sanjeev Sharma
Relational Algebra
• The relational algebra is a procedural theoretical language with
operations that work on one or more relations to define another
relation without changing the original relation.

• Thus, both the operands and the results are relations; hence the
output from one operation can become the input to another
operation.
• This allows expressions to be nested in the relational algebra.
This property is called closure. Relational algebra is an
abstract language, which means that the queries formulated in
relational algebra are not intended to be executed on a
computer.
Relational Algebra Operations
• Operations may be unary or binary.
• The functional operations in the relational algebra are :
– Select
– Project
– Union
– Set difference
– Cartesian product
– Rename
– Intersection
– Division
– Join
– Natural Join
Selection Operation
• The selection operation works on a single relation R and
defines a relation that contains only those tuples of R that
satisfy the specified condition (Predicate).

• Selection operation can be considered as row wise filtering.

• The syntax of selection operation is:


σ Predicate (R)
• Here R refers to relation and predicate refers to condition.
Example database
• Ex. If you want all the employees having salary more than
9,000 from relation Employee. The query, is
• Note: We can use following predicate.
– = (equal to)
– < (less than)
– > (greater than)
– ≠(not equal to)
– ≤ (less than or equal to)
– ≥(greater than equal to)

– We can use AND and OR to combine predicate


– ˅ OR
– ˄ AND
Project
• The projection operation is a unary operation which applies
only on a single relation at a time. Project operation is used to
select vertical subset of relation (i.e., columns of table). It is
denoted by pi (π).
• Consider If you want all the names of employees and their
salary from relation employee. Then query, is

π name, salary (Employee)

Output of the query is:-


Basic Set-oriented Operations
• The union operation : The union operation is a binary
operation that is used to find union of relations. Here relations
are considered as sets. So, duplicate values are eliminated. It is
denoted by (∪).
• Conditions for union operation : There are two necessary
conditions for union operation.
– (i) Both the relations have same number of attributes.
– (ii) Data types of their corresponding attributes must be same.
• Two relations are said to be union compatible if they follow
the above two conditions.
• Ex. If you want to find the names of all employees and
names of all students together
• then the query is
πName (Employee) ∪ πName (Student)
Relational Model

Presented By:-
Dr. Sanjeev Sharma
Basic Set-oriented Operations
• The union operation : The union operation is a binary
operation that is used to find union of relations. Here relations
are considered as sets. So, duplicate values are eliminated. It is
denoted by (∪).
• Conditions for union operation : There are two necessary
conditions for union operation.
– (i) Both the relations have same number of attributes.
– (ii) Data types of their corresponding attributes must be same.
• Two relations are said to be union compatible if they follow
the above two conditions.
• Ex. If you want to find the names of all employees and
names of all students together
• then the query is
πName (Employee) ∪ πName (Student)
Intersection
• Set intersection operation : Set intersection is used to find
common tuples between two relations. It is denoted by (∩).
• If you want to find all the employees from Relation Employee
those are also students. Rules of set union operations are also
applicable here. Then the query, is
πName (Employee) ∩ πName (Student)
Difference
• Set-difference operation : Set-difference operation is a binary
operation which is used to find tuples that are present in one
relation but not in other relation. It is denoted by (—). It
removes the common tuples of two relations and produce a
new relation having rest of the tupels of first relation.
• Ex. If you want the names of those employees that are not
students, then the query, is
πName (Employee) — πName (Student)
• Cartesian product operation : Cartesian product is a binary
operation which is used to combine information of any two
relations. Suppose a relation R1 is having m tuples and other
relation R2 is having n tuples then R1 × R2 has m × n tuples. It
is denoted by (X).

• Consider the Figure. Cartesian product of relation Employee


and Job is shown in Figure .

Query is → Employee × Job


Join Operations
• Join operation combines two relations to form a new relation.
The tables should be joined based on a common column. The
common column should be compatible in terms of domain.

• Types of Join Operations


Natural Join
• The natural join performs an equi join of the two relations R
and S over all common attributes. One occurrence of each
common attribute is eliminated from the result. In other words
a natural join will remove duplicate attribute.

• Input: Two relations (tables) R and S


• Notation: R S
• Purpose: Relate rows from second table and
• – Enforce equality on all column attributes
• – Eliminate one copy of common attribute
• In this result, information about Ramesh, Jack, Vijay, Gaurav
are missing. So, outer join is used to avoid this loss of
information.
Thank You
Relational Model

Presented By:-
Dr. Sanjeev Sharma
• There are Three types of outer joins.
• (i) Left outer join : It is used to take all tuples of relation that
are on the left side whether they are matching with tuples of
right side relation or not. It is denoted by ( ).
• Right outer join : It is used to take all tuples of relation
that are on the right side whether they are matching with tuples
of left side relation or not.
• Full outer join : It is used to take all tuples from left and right
relation whether they match with each other or did not match.
Theta (θ) join
• θ in Theta join is the join condition. Theta joins combines
tuples from different relations provided they satisfy the theta
condition.

• R1 ⋈θR2

• R1 and R2 are relations with their attributes (A1, A2, .., An )


and (B1, B2,.. ,Bn) such that no attribute matches that is R1 ∩
R2 = Φ Here θ is condition in form of set of conditions C.
Equi Join
• A special case of condition joins where the condition C
contains only equality.

• Example of Equi Join


• Given the two relations STAFF and DEPT, produce a list of
staff and the departments they work in.
Thank You
Relational Algebra Operations
Rename Operation
Assignment Operation
Division Operation
SQL
• Some of the features of SQL are:
• – SQL is a language used to interact with the database.
• – SQL is a data access language.
• – SQL is based on relational tuple calculus.
• – SQL is a standard relational database management language.
• – The first commercial DBMS that supported SQL was Oracle
in 1979.
• – SQL is a “nonprocedural” or “declarative” language.
Data Manipulation in SQL
• SQL has one basic statement for retrieving information from the
database:

• The SELECT statement.


• SQL also provides Three other DML statements to modify the
database.

• These statements are: update, delete and insert.

• The basic form of the SELECT statement is formed of three clauses


SELECT, FROM, and WHERE, having the following form:
Select
• Example 1 : To display all department ID and the Department
name, the query is
• Example 2 : To select all columns use “*”.
• Column Alias : You can give name to columns of your choice
by using keyword “As” (gives column name in upper-case
letter) or by using “ ” (gives column name as specified in
query).
• Concatenation Operator (II) : It is used to concatenate two
or more columns.
• Eliminating Duplicate Rows : To eliminate duplicate rows,
the keyword „DISTINCT‟ is used.
• Arithmetic Operators and NULL Values : SQL provides
arithmetic operators to perform calculations. Arithmetic
operators with their precedence are

• A null value is unknown value and it is different from zero.


Any arithmetic operation with null value gives null results.
• Suppose you want to increase salary of each employee by
500.
• Where Clause : WHERE clause is used to select particular
rows from table.
• Comparison or relational operators : The relational operators
provided by SQL are as follows.
• Special operators : Special operators provided by SQL are
as follows :
• List name and EID of employees having salary ` 8000 or `
9500.
• List the EID and names of employees who were hired by
company from 5–Feb–2001 to 1–Jan–2006.
• List the EID and names of employees having MID equal to
null.
• Two symbols with L
• (i) % → It represents any sequence of zero or more characters.
• (ii) _ (underscore) → It represents any single character.IKE
operator can be used:
• List the names of employees ending with „it‟.
• List the names of employees having second
alphabet of their names is „a’.
• (c) Logical operators : Logical operators are
used to combine two conditions. Following
• are the logical operators provided by SQL.
SQL Part 2
Create Table
Insert Table
Ordering Data
Create Table from Table
Inserting Data into a Table from
Another Table
Delete Operations
Update Operation
ALTER Table
Renaming Command
Drop Command
Unique Key Constraints
Foreign Key
SQL
Order By Clause
• Order by Clause : Order by clause is used to sort rows in both
ascending and descending order.
• (i) ASC : To sort rows in ascending order (By default).
• (ii) DESC : To sort rows in descending order.
Joining of Tables
• If we need information from more than one table then we use
joins. To join tables the condition need to be specified.
• 1. Cartesian Product : In Cartesian product there is no join
condition. It returns all possible combinations of rows.
• Equijoin : When two or more tables are joined by equality of
values in one or more columns then it is called Equijoin.
• Table Aliases : Alias names can be given to the tables. It is
specified in FROM clause. They are helpful to simplify
queries.
Outer Join
• The outer join operator is ‘(+)’. During simple joining some
rows are missing due to null value. To display these rows use
outer join operator towards defficient side.
Relational Database Design
• Check the following FD?
Relational Database Design
Relational Database Design
Relational Database Design
Normalization of Relations (1)
• Normalization:
– The process of decomposing unsatisfactory
"bad" relations by breaking up their attributes
into smaller relations

• Normal form:
– Condition using keys and FDs of a relation to
certify whether a relation schema is in a
particular normal form
Normalization of Relations (2)
• 2NF, 3NF, BCNF
– based on keys and FDs of a relation schema
• 4NF
– based on keys, multi-valued dependencies :
MVDs;
• 5NF
– based on keys, join dependencies : JDs
• Additional properties may be needed to
ensure a good relational design (lossless
join, dependency preservation; see Chapter
15)
Definitions of Keys and Attributes
Participating in Keys (1)
• A superkey of a relation schema R = {A1, A2, ...., An} is a set
of attributes S subset-of R with the property that no two tuples
t1 and t2 in any legal relation state r of R will have t1[S] =
t2[S]

• A key K is a superkey with the additional property that


removal of any attribute from K will cause K not to be a
superkey any more.
Definitions of Keys and Attributes
Participating in Keys (2)
• If a relation schema has more than one key, each is called a
candidate key.
– One of the candidate keys is arbitrarily designated to be
the primary key, and the others are called secondary keys.

• A Prime attribute must be a member of some candidate key

• A Nonprime attribute is not a prime attribute—that is, it is


not a member of any candidate key.
Relational Database Design
Relational Database Design

You might also like