Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

DBMS Unit (1,2,3)

This document provides an introduction to database management systems. It defines key concepts such as data, information, databases, and DBMS. It states that a database is a systematic collection of related data, and gives examples like YouTube and Instagram. A DBMS is a program that allows users to create, access, and manage the data in a database. The document discusses why DBMS are needed and the disadvantages of using file systems to manage data. It also outlines the basic components of a database system including hardware, software, data, users, and procedures.

Uploaded by

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

DBMS Unit (1,2,3)

This document provides an introduction to database management systems. It defines key concepts such as data, information, databases, and DBMS. It states that a database is a systematic collection of related data, and gives examples like YouTube and Instagram. A DBMS is a program that allows users to create, access, and manage the data in a database. The document discusses why DBMS are needed and the disadvantages of using file systems to manage data. It also outlines the basic components of a database system including hardware, software, data, users, and procedures.

Uploaded by

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

Introduction to

Database
Management system
Lecture-1
DATA and INFORMATION
• To understand what is database, first we need a brief understanding
of about DATA and INFORMATION

• DATA
Data: In simple words, data is raw, isolated facts about an entity or
an object which is stored in recorded form. For example, text, audio,
image, video etc.
• INFORMATION
Information: It is a processed, meaningful and usable data.
DATA and INFORMATION
5 is a DATA or
INFORMATION?

a. DATA

b. INFORMATION
“You can have data without
information, but you cannot have
information without data.” —
Daniel Keys Moran
DATABASE and DBMS

Database System

Database DBMS

Microsoft: SQL
Server, Oracle
Structured Unstructured
(9i,10g,11c,12i),
MySQL, IBM: DB2
DATABASE
• Companies have their own Database System.
• Structure Data: Database that can be stored in some particular
structure.
• Structure used for structured database is RDBMS.
• Unstructured Data: Database that has no particular structure to store
data
DBMS

• A lot of data is stored in Hard drives, but what to do with it?

• Users will fetch the data and will perform the operations: update,
insert, delete.

• To perform all these operations there must be a system- DBMS

DBMS provides operations using which users can easily perform


operations on data.
DATABASE and DBMS

• Database: It is a systematic collection of similar or related data. If the


data is not related, we cannot extract the relevant information from
it.
• Database makes data management easy.
• For example, video is data, but YouTube is a database.
• Image is data but Instagram is a database. (as a collection of related
data)
DATABASE and DBMS

• Database Management System: It is a program which enables the


user to create, manipulate and delete database.
• It is also used for controlled access to database.
Database Management System
Why we need database management system?

Because there are disadvantages of using file system


Data Inconsistency
Difficulty in accessing
Data Redundancy
data

Atomicity Problem Data Isolation

Concurrent access
Security Problems
anomalies
a situation where there are multiple tables within a
database that deal with the same data but may receive it
from different inputs.
Data Inconsistency

If we have an address of someone in many tables and


when we change it in only one table and in another table it
may not be updated so there is the problem of data
inconsistency may occur.
Atomicity Atomicity is a feature of databases systems where a
transaction must be all-or-nothing.

That is, the transaction must either fully execute


and complete, or not execute at all.

It must not complete partially.


Data Isolation Database isolation refers to the ability of a database
to allow a transaction to execute as if there are no
other concurrently running transactions

when a customer uses your billing portal, they


are essentially creating a sub-account with the
same permissions as everyone else, but it exists
separately from the rest of the network
Operations on Database
•To add new information
• To view or retrieve the stored information
• To modify or edit the existing
• To remove or delete the unwanted information
• Arranging the information in a desired order etc.
Components of Database system
• There are five major components of database system
• Hardware
• Software
• Data
• Users
• Procedures
Applications of Database
• Banking: transactions
• Railway: reservations and schedules
• Online retailing: order submit, order tracking, customized
recommendations etc.
• Manufacturing: Inventory, productions, etc.
And so on
• University Management System
(UMS) is an application of
Database
• Yes
• No
QUIZ time
Components of Database
Five major components in database system environment:
Hardware
Software
Data
Users
Procedures
Components of Database System
Hardware: It is the actual computer system used for keeping and accessing the database.

DBMS hardware consists of secondary storage devices like hard disks.

Software: It is the actual DBMS. Between the physical database itself and the users of

system is a layer of software, called DBMS.

Data: Data acts as the bridge between the machine components and user components.
Components of Database System
Users: There are number of users who can access or retrieve
data on demand using the applications and the interfaces provided
by DBMS. The users can be:
• Naive users
• Online users
• Application Programmers
• Sophisticated Users
• Data base Administrator ( DBA)
Different Types of Database Users in DBMS

1. Application Programmers
• As its name shows, application programmers are the one who writes application
programs that uses the database.

• These programs meet the user requirement and made according to user requirements.

• Retrieving information, creating new information and changing existing information


is done by these application programs.
Different Types of Database Users in DBMS
1. Application Programmers

They interact with DBMS through DML (Data manipulation language) calls. And all
these functions are performed by generating a request to the DBMS.
End Users
End users are those who access the database from the terminal end.
They use the developed applications and they don’t have any
knowledge about the design and working of the database.
Their main motto is just to get their task done.

There are basically two types of end users:


• Casual User
• Naive
Casual User
• These users have great knowledge of query language.
• Casual users access data by entering different queries from the terminal end.
• They do not write programs but they can interact with the system by writing
queries.
Naive
• Any user who does not have any knowledge about database can be in this category.
• Their task is to just use the developed application and get the desired results.
• For example: Clerical staff in any bank is a naive user.
• They don’t have any dbms knowledge but they still use the database and perform
their given task.
Sophisticated Users - They are database developers, who write SQL
queries to select/insert/delete/update data. They do not use any application
or programs to request the database. They directly interact with the
database by means of query language like SQL.

DBA (Database Administrator)


DBA can be a single person or it can be a group of person. Database
Administrator is responsible for everything that is related to database.
Makes the policies, strategies and provides technical support.
System Analyst
System analyst is responsible for the design, structure and
properties of database. All the requirements of the end users are
handled by system analyst. Feasibility, economic and technical
aspects of DBMS is the main concern of system analyst.
Architecture of Database Management System
1-tier or Single-tier Architecture
The simplest of database architecture is 1 tier where the Client,
Server, and Database all reside on the same machine.
But such architecture is rarely used in production.
2-tier Architecture
Two-tier is based on Client-Server architecture.

In this type of architecture we have:

• Presentation layer which runs on client (PC, mobile, tablet etc.)

• Data is stored on server

An application interface which is called ODBC (Open Database Connectivity)

an API which allows the client-side program to call the DBMS.


2-Tier Architecture
3-Tier Architecture
3-tier schema is an extension of the 2-tier architecture. 3-tier architecture has the

following layers:

• Presentation layer (your PC, Tablet, Mobile, etc.)

• Application layer (server)

• Database Server

It can be used in web applications and distributed applications.

It is the most widely used architecture to design a DBMS.


Two-tier and three-tier architecture
3-Tier Architecture
When you install a DB in your
system and access it to practice
SQL queries is which type of
architecture?
A Contact Management
System created using MS-
Access
Any large website on the
internet
Homework:

When you book a train ticket


online follow which type of
architecture?
Schemas, Instances, and Database State

◼ Database Schema
 The description of the database
 Specified during database design
 Expected to change when requirements change
◼ Schema Diagram
 Most data models have certain conventions for displaying schemas as
diagrams
 Displays only some aspects of a schema, such as names of record types
and data items
 Some constraints are difficult to represent
◼ Schema Construct
 Each object in the schema (e.g., student, course)
Example
Database State or Snapshot

◼ Actual data in the database may change frequently


◼ The data in the database at a particular moment in time is called a
database state or snapshot
◼ Also called the current set of occurrences or instances in the database
Database Schema vs. Database State

◼ The distinction is very important

◼ When we define a database, we specify its database schema only to the


DBMS
 Database state is the empty state (w/ no data)
 Initial state: when the database is first populated or loaded with
initial data
 Current State: State at any point in time. Every time an update operation is
applied to the database, database state changes
Database Schema vs. Database State

◼ Valid State: A state that satisfies the structure and the


constraints specified in the schema

◼ The schema is sometimes called the intension and a database


state is called an extension of the schema
Three-Schema Architecture
The goal is to separate user applications and physical database
Defines DBMS schema at three levels:

Internal schema at the internal level to describe data storage structures and
access paths. Typically uses physical data model.
Conceptual schema at the conceptual level to describe the structure and
constraints for the whole data se. Uses a conceptual or an implementation data
model.
External schema at the external level to describe the various user views.
Usually uses the same data model as the conceptual level.

Mapping: Process of transforming requests and results between levels


Example: University Management System
Levels of Abstraction

◼ A major purpose of database system is to provide users with an abstract view of


the data

◼ Physical level: describes how a record (e.g., customer) is stored.

◼ Logical level: describes data stored in database, and the relationships among
the data.

◼ View level: application programs hide details of data types. Views can also
hide information (such as an employee’s salary) for security purposes.
Three Level Architecture of DBMS
Three Level Architecture of DBMS

◼ External Level
◼ Conceptual Level
◼ Internal Level
External Level or View level

▪ It is the users’ view of the


database.
▪ It describes that part of the
database that is relevant to
each user.
▪ It is closest to the end users.
▪ Externallevel is also known as
the view level.
External Level or View level
▪ It deals with the way in which
individual users view data.
▪ Individual users are given different
views according to their requirements.
▪ Example: one user may view dates in
the form (day, month, year), while
another may view dates as (year,
month, day).
▪ One may be interested in First Name
other may be in First Name and Last
Name.
Conceptual Level or Logical Level
▪ This level must not contain any storage
dependent details.
▪ For example it contains only
information about data type and its size
but not any storage considerations,
such as the number of bytes occupied.
▪ Conceptual level is also known as the
logical level.
Conceptual Level or Logical Level

This level deals with


◼ All entities, their attributes, and
their relationships
◼ The constraints on the data
◼ Security and integrity information
Internal Level or Storage level

▪ It is the physical representation of


the database on the computer.
▪ This level describes how the data
is stored in the database.
▪ It concerns the way the data are
physically stored on the
hardware.
Internal Level or Storage level

The internal level is concerned with


▪ Storage space allocation for data
and indexes;
▪ Record descriptions for storage
(with stored sizes for data items);
▪ Record placement;
▪ Data compression and data
encryption techniques.
Three Level Architecture
Mapping between the Views
Mapping between Views
External/Conceptual Mapping:
◼ Each external schema is related to the
conceptual schema by the
external/conceptual mapping.
◼ It maps logical record in the external
view to one (or more) conceptual
record(s) in the conceptual view.
Mapping between Views
Conceptual/Internal Mapping:
◼ Conceptual schema is related to the
internal schema by the
conceptual/internal mapping.
◼ This enables the DBMS to find the
actual record or combination of records
in physical storage that constitute a
logical record in conceptual schema.
Schema

▪ The overall description of the database is called the


Database Schema.
▪ There are three different types of schema in the database
corresponding to each data view of database. In other words,
the data views at each of three levels are described by schema.
Schema

▪ The External view is described by means of a schema called external schema that
correspond to different views of the data.
▪ Similarly the Conceptual view is defined by conceptual schema, which describes all
the entities, attributes, and relationship together with integrity constraints.
▪ Internal View is defined by internal schema, which is a complete description of the
internal model, containing definition of stored records, the methods of representation,
the data fields, and the indexes used.
Data Independence
Achievement of Three Level Architecture
Data Independence

◼ Application Programs should be independent from the way


the data is stored and accessed.
Data Independence-Achievement of Layered
Architecture of DBMS
There are two kinds of data independence:
◼Logical data independence
◼Physical data independence
Logical data independence

Logical data independence indicates that the conceptual schema can be


changed without affecting the existing external schemas. The change
would be absorbed by the mapping between the external and conceptual
levels.
Physical data independence

Physical data independence indicates that the physical storage


structures or devices could be changed without affecting
conceptual schema. The change would be absorbed by the
mapping between the conceptual and internal levels.
Procedure for Data Access by DBMS
Procedure for Data Access by DBMS
Thanks
DBMS Basic Concepts
DBMS
• Database Management System
• Term Database requires understanding of
data and information
• Data: It can be anything like name, place or number, etc.
Data usually refers to raw data, or unprocessed data.

• Information: It is organized or classified data so that it has


some meaningful values to the receiver.
– Information is the processed data on which decisions
and actions are based.
Difference between Data and
Information?
Data Information
Data is raw facts and figures Information is a processed form of data

For example: 12 is data For example: When 12 is stored in row


column form as shown it is information.
Age 12

Data are atomic level pieces of Information is a collection of data


information

Data does not help in decision making Information helps in decision making
Database
• A database is a collection of logically related data that is
designed to meet the information needs of an
organization
• The related information when placed is an organized
form makes a database.
• The organization of data/information is necessary
because unorganized information has no meaning.
Purpose of DBMS
An example
• University Database:
Data about students, faculty, courses, research-laboratories,
course registration/enrollment etc. Reflects the state of
affairs of the academic aspects of the university.
Purpose: To keep an accurate track of the academic
activities of the university.
Purpose of DBMS
Before DBMSs were introduced, organizations usually stored
information in file processing system which has a number of
disadvantages:
• Data redundancy and inconsistency
• Difficulty in accessing data
• Data isolation
• Integrity problems
• Atomicity problems
• Concurrent-access anomalies
• Security problems
Database Management System
• DBMS A database management system is the software
system that allows users to define, create and maintain a
database and provides controlled access to the data.
• A database management system (DBMS) is basically a
collection of programs that enables users to store,
modify, and extract information from a database as per
the requirements.
Operations on databases
• To add new information
• To view or retrieve the stored information
• To modify or edit the existing information
• To remove or delete the unwanted information
• Arranging the information in a desired order etc.
Components of Database
• Five major components in database system environment:
– Hardware
– Software
– Data
– Users
– Procedures
Components of Database System
• Hardware: It is the actual computer system used for keeping
and accessing the database. DBMS hardware consists of
secondary storage devices like hard disks.

• Software: It is the actual DBMS. Between the physical


database itself and the users of system .
• This is the program which controls everything. The DBMS
software is more like a wrapper around the physical database,
which provides us with an easy-to-use interface to store,
access and update data.
Components of Database System
• Users: There are number of users who can access or retrieve
data on demand using the applications and the interfaces
provided by DBMS. The users can be:
• Data base Administrator ( DBA)
• Application Programmers
• End Users: These are divided into:
–Casual End User
– Naïve End User
–Sophisticated user
Components of Database System
• Procedures: It refers to the instructions and rules that govern
the design and the use of the database. The users of the
system and the staff that manage the database requires
documented procedures on how to use or run the system.

• Data: It is that resource, for which DBMS was designed. The


motive behind the creation of DBMS was to store and utilize
data.
In a typical Database, the user saved Data is present and meta
data is stored.
Applications of DBMS
• 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
Data models, Schemas, and Instances
• Data model:-A set of concepts to describe the structure of a
database, and certain constraints that the database should
obey.
• Schema:- The overall description of the database is called the
Database Schema.
– A schema is defined as an outline or a plan that describes
the records and relationships existing at the particular
level.
• Instance:- Data in the database at a particular moment in
time.
Database Architecture
DBMS Architecture
• DBMS architecture helps in design, development,
implementation and maintenance of the database.
• The design of a DBMS depends on its architecture. It can
be
– Centralized
– Decentralized
– Hierarchical.
DBMS Architecture
• The architecture of a DBMS be either single tier or multi-
tier.
• Selecting the correct database architecture helps in quick
and secure access to this data.
• The tiers are classified as follows :
– 1-tier
– 2-tier
– 3-tier
1-tier or Single-tier Architecture
• The simplest of database architecture is 1
tier where the Client, Server, and Database
all reside on the same machine.
• But such architecture is rarely used in
production.
Two-tier and three-tier architecture
Two-tier architecture
Client Client Client
machine 1 machine 1 machine 1

Database
Server
Three-tier architecture
User
Client Client Client Client Application or Client Layer
Application machine 1 machine 1 machine 1

Business Layer
Application Database Server
Server

Data Server Data Layer


Database
Server
When you install a DB in your
system and access it to practice
SQL queries is which type of
architecture?
Answer:

1-Tier Architecture
• A simple one tier architecture
example would be anytime you
install a Database in your system
and access it to practice SQL
queries. But such architecture is
rarely used in production.
When you book train ticket
online follows which type of
architecture?
Schema
• It is logical representation of database. Or
• Overall design of the database is called a schema
• Data is logically represented in the form of tables.
Example: Design schema of entity ‘student’.
Reg. Number Name Age Address

In hard disk data is not stored in the form of Reg. number,


name etc..
In DBMS, when data will be accessed, it will be accessed in
the form of tables.
Schema
• The term database schema can refer to a visual representation of
a database, a set of rules that govern a database.
• Rules and constraints to define the database

Important Point

• A database schema is a sketch of a planned database. It doesn’t


have any data in it.
Instance
• When schema is filled with data items it is termed as
instance of schema.
• Data in the database at a particular moment of time is
called database state or snapshot, instance.

Reg. Number Name Age Address


1234 ABC 12 xyzzy
Three level schema architecture
This gives rise to a term
called Data Independence
Objective of 3 level architecture:
is to separate each user’s view of database from the way
the database is physically represented.
Reason:
1. Each user may be need to see data as per need
2. Users need not to directly deal with physical database
storage details
3. DBA should be able to change DA storage structures without
changing the user’s view
• External or View level: It is the users’ view of the
database. This level describes that part of the database
that is relevant to each user.
– For example, one user may view dates in the form
(day, month, year), while another may view dates as
(year, month, day).
• Conceptual or logical level: It is the community view of
the database. This level describes what data is stored in
the database and the relationships among the data.
• It represents:
– All entities, their attributes, and their relationships;
– The constraints on the data;
– Security and integrity information.
• Physical Schema (Internal or storage level)
It is the physical representation of the database on the
computer. This level describes how the data is stored in the
database.
3 level schema architecture
Data Independence or Data Abstraction

• A major purpose of database system is to provide


user with an abstract view of data.

• That is, system hides certain details of how the data


are stored and maintained.
View of Data

An architecture for a database system


Levels of Abstraction(view of data)
• View level: describe only part of database.
• Application programs hide details of data types.
• Complexity remain due to variety of information stored.
• Views can also hide information (such as an employee’s
salary) for security purposes.
Levels of Abstraction(view of data)

• Logical level: describes what data stored in database,


and the relationships among the data.
Database Designers, who decides what information to keep
in the database, use the logical level of abstraction.

• Physical level: describes how a record (e.g., customer) is


stored.
DBA use this view.
Data Independence-Achievement of Layered
Architecture of DBMS

• Two kinds of data independence:


– Logical data independence
– Physical data independence
Data Independence
• Logical Data Independence: The capacity to change
the conceptual schema without having to change the
external schemas and their application programs.
• Physical Data Independence: The capacity to change
the internal schema without having to change the
conceptual schema.
3 level schema architecture
Mapping between Views
• The processes of transforming requests and results
between the levels are called mappings.

• When a schema at a lower level is changed, only the


mappings between this schema and higher-level
schemas need to be changed in a DBMS.
• The higher-level schemas themselves are unchanged.
Hence, the application programs need not be changed
since they refer to the external schemas.
Procedure for Database Access
Database Models
ER DIAGRAM
• ER model was introduced by Peter Chenn in 1976
• The ER model defines the conceptual (or logical) view
of a database.
• It is used for designing database.
• It works around real-wor ld entities and the relationships
among them
• A database schema in the ER Model can be
represented pictorially as ER diagrams
• An ER diagram maps well into a relational schema.
WHY ER DIAGRAM
ER Modeling Constructs
1. Entity
2. Attribute
3. Relationship
Entity
An Entity is a "thing" or "obiect" in the real world that is
distinguishable f rom other objects.

Entity can be any -thing that has an independent existence


and about which
we collect data. It is also known as entity type.

Example: I n a School database, the students,teac hers,


classes, courses, or proj ects can be taken as an entity
Entity
Entities are represented by means of rectangles.

• Entities have attributes that give them their identity


• Example: Students have roll. no., names and addresses
• Entities becomes table in relational model
Entity Set
An Entity Set is a collection of similar type of entities that
share same attributes.
Example: a Student’s set may contain all the students of a
school;
A Teachers set may contain all the teachers of a school from all
faculties.
Entity Set
Entity sets need not be disioint
Example: the entity set Employee (all employees of a bank)
and the entity set Customer (all customers of the bank) may
have members in common
Attribute
An entity. is represented by a set of attributes
Attributes are used to describe the property of an entity
• Example: a Student entity may have Roll_No, Name,
DOB,Age,Address, Mobile No as attribute
For eac h attribute there is a set of permitted va lues,called
domain (or range) of that attribute
Attribute
Example: a student 's name cannot be a numeric va lue. It has
to be alphabetic. A student's age cannot be negative, etc. A
student roll_no can ne numeric between some range like (0-
10000)
Attributes are represented by Ellipse

Attribute
Attribute
Types of Attributes
• Simple and composite attributes
• Single-valued and multi-valued attributes
• Stored and Derived attributes
Relationships
• A Relationship is an association among entities For
example:
1. an employee works_at a department
2. a student enrolls in a course.
• Here, Works_at and Enrolls are called relationships.
• Relationships are represented by diamond-shaped box
Relationships
Relationship Set
A set of relationships of similar ty pe is called relationship
Set
The following relationship set "Enrolls(E1,E2,E3)" depicts:
S1 is enrolled in C2,S2 is enrolled in Cl and S3 is enrolled in
C3
Relationship Descriptive Attribute
Like entities, a relationship too can have attributes . These
attributes are called descriptive attributes
For instance, the depositor relationship set between entity
sets customer and account may have the attribute access-
date
Degree of Relationship
The number of different entity sets participating in a
relationship set is called as degree of a relationship set
1. Unary
2. Binary
3. Ternary
4. N-ary
Degree of Relationship
Unary Relationship:{degr ee = 1)
A unary relationship is only one entity participate in a
relationship, the relationship is ca lled as unary relationship.
1) For example,one person is married to only one
person
Degree of Relationship
Binary Relationship: (degree = 2 )
A binary relationship is when two entities participate in a
relationship, and is the most common relationship degree.
2) For example, Student is enrolled in Course
Degree of Relationship
Mapping Cardinality
• One-to-One
• One-to-Many
• Many -to-One
• Many -to-Many
Attribute Types
Attributes in ER Model
An Entity is a "thing" or "object" in the real world that is
distinguishable from other objects.
for eg: Student, Teacher, Class, Course, Employee, Customer,
account

An entity is represented by a set of attributes

Attribute is used to describe the property of an entity


Attributes in ER Model
Ex. a Student entity may have Roll_No, Name, BirthDate, Age as
attribute
There exists a domain or range of values that can be assigned to
attributes.
For example, a student's name cannot be a numeric value. It has
to be alphabet. A student's age cannot be negative, etc
Attribute Types
• Simple and Composite attributes
• Single-valued and Multi-valued attributes
• Stored and Derived attributes
• Key Attribute
Attribute Types
• Simple and Composite attributes
• Single-valued and Multi-valued attributes
• Stored and Derived attributes
• Key Attribute
Attribute Types
Attribute Types
Attribute Types
Attribute Types
Attribute Types
Attribute Types
Attribute Types
Attribute Types
Mapping Cardinality Constraints

Express the number of entities to which another


entity can be associated via a relationship set.
• Most useful in describing binary relationship sets.
•For a binary relationship set the mapping
cardinality must be one of the following types:
– One to one
– One to many
– Many to one
– Many to many
Mapping Cardinality Constraints
Mapping Cardinality Constraints
One to One
One to Many
Many to One
Many to Many
How to choose appropriate relationship
How to choose appropriate relationship
Participation Constraints
Participation Constraints
What is a Schema

• Take Home reading task


Database Languages
Database languages are used to create and maintain database on
computer.
• Data Definition Language(DDL): It is a language that allows
user to define data and their relationship to other types of
data.
– CREATE
– ALTER
– DROP
– TRUNCATE
– RENAME
Database Languages
• Data Manipulation Language(DML):It provides a set of
operations to support the basic data manipulation operations
on the data held in databases. It allows user to insert, update,
delete and retrieve data from the database.
– DELETE
– INSERT
– SELECT
– UPDATE
Database Languages
• Data Control Language(DCL): DCL statements control
access to data and the database
– GRANT
– REVOKE
– COMMENT
Database Languages
• Transaction Control Language(TCL): TCL statements manage
the change made by DML statements, and group DML
statements into transactions
– COMMIT
– ROLLBACK
– SAVEPOINT
– SET TRANSACTION
Structure and Component of DMBS
Generalization, Specialization and
Aggregation in ER Model
Generalization
• Generalization is the process of extracting common properties
from a set of entities and create a generalized entity from it.
• Generalization is like a bottom-up approach in which two or
more entities of lower level combine to form a higher level entity
if they have some attributes in common.
• Generalization is more like subclass and superclass system, but
the only difference is the approach.

• For Example, STUDENT and FACULTY can be generalized to a


higher level entity called PERSON.
Generalization
Specialization
• Specialization is a top-down approach, and it is opposite to
Generalization.
• In specialization, one higher level entity can be broken down into
two lower level entities.
• Specialization is used to identify the subset of an entity set that
shares some distinguishing characteristics.
• For example: In an Employee management system, EMPLOYEE
entity can be specialized as TESTER or DEVELOPER based on what
role they play in the company.
Specialization
Aggregation
• In aggregation, the relation between two entities is treated as a
single entity.
• In aggregation, relationship with its corresponding entities is
aggregated into a higher level entity.

• For example: Center entity offers the Course entity act as a


single entity in the relationship which is in a relationship with
another entity visitor.
• In the real world, if a visitor visits a coaching center then he will
not enquire about the Course only or just about the Center
instead he will ask the enquiry about both.
Aggregation
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables

In total 3 tables will be required:


• One table for relationship Schema
• Second table for entity Employee 1. Worksin(Emp_no, Dept_id, since)
• Third table for entity Department 2. Employee(Emp_no, Emp_name, Salary)
But to represent relationship only one 3. Department(Dept_id, Dept_name)
table is required.
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables

In one to many, second table is created by merging second entity and relationship.
a1 will be a foreign key
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
Example: Converting ER Diagrams to Tables
1. Find maximum relations
2. Find minimum relations
Example Question

Find the minimum


number of tables
for this scenario?
Relational Algebra
Relational Algebra
Relational Algebra
Relational Algebra Operations
Selection Operator
Selection Operator
Selection Operator

Here D is written in “ ” because it is character, numbers are not


written in “ ”
Selection Operator
Selection Operator
Selection: Examples
Selection: Examples
Projection Operator
Projection Operator
Projection Operator
Projection Operator
Projection Operator
Projection Operator
Set Operators
Set Operators
Set Operators
Example: Union
Example: Union in projection
Example: Union in projection
Set Intersection Operators
Set Intersection Operators
Set Intersection Operators
Set Intersection Operators
Set Difference (-) Operator
Set Difference (-) Operator
Set Difference (-) Operator
Cartesian Product or Cross Product
Cartesian Product or Cross Product
Characteristics: Cartesian Product
Cartesian Product or Cross Product

Attributes of R1 and R2 are different. Suppose R2 has same named attribute say
B, then attribute in R2 will be written as “R2.B”.
Cartesian Product or Cross Product
Attributes of R1 and R2 are different. Suppose R2 has same named attribute say
B, then attribute in R2 will be written as “R2.B”.
Composition of Operations
Composition of Operations
Composition of Operations
Composition of Operations
Example:
SQL
SQL

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization
(ISO) in 1987
SQL
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and
views
Types of DBMS
languages
Types of DBMS languages:

• Data Definition Language (DDL): DDL is used for


specifying the database schema.
• To create the database instance – CREATE
• To alter the structure of database – ALTER
• To drop database instances – DROP
• To delete tables in a database instance –
TRUNCATE
• To rename database instances – RENAME
• All these commands specify or update the database
schema : Data Definition language.
• Data Manipulation Language (DML): DML is used
for accessing and manipulating data in a database.
• To read records from table(s) – SELECT
• To insert record(s) into the table(s) – INSERT
• Update the data in table(s) – UPDATE
• Delete all the records from the table – DELETE
• Data Control language (DCL): DCL is used for
granting and revoking user access on a database –
• To grant access to user – GRANT
• To revoke access from user – REVOKE
• In practical data definition language, data
manipulation language and data control languages
are not separate language; rather they are the parts
of a single database language such as SQL.
Transaction Control Language:

• It manage the changes made by DML .


• COMMIT-make transaction changes permanent
• ROLLBACK-undo changes made by transaction
either since it started or since save point
• SAVEPOINT-set point to which transaction can be
rolled back
• SET TRANSACTION-establish properties for
transaction
SQL QUERIES-1

DDL-COMMANDS
CREATE
• SQL CREATE TABLE statement is used to create
table in a database.
• If you want to create a table, you should name the
table and define its column and each column's data
type.
• To create the table:
• create table table-name (column-name1 datatype1,
column-name2 datatype2, column-name3
datatype3, column-name4 datatype4 );
create table STUDENTS(id number(10), name
varchar(20), age number(4));
• You can verify it, if you have created the table
successfully by looking at the message displayed by
the SQL Server, else you can use DESC command as
follows:
• SQL> DESC STUDENTS;

Practice:
Create a table Employee having attributes as id,
name, department, age.
Inserting Data into Table

1. Specify both the column names and the


values to be inserted:
INSERT INTO table_name (column1, column2,
column3, ...)
VALUES (value1, value2, value3, ...);
Inserting Data into Table
2. If you are adding values for all the columns of
the table, you do not need to specify the column
names in the SQL query.
However, make sure the order of the values is in
the same order as the columns in the table. Here,
the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
Inserting Data into Table

• Insert command is used to insert data into a table.


Following is its general syntax,
• INSERT into table-name values(data1,data2,..);
• Eg: INSERT into STUDENTS values(101, ‘Adam’ ,15);
2.Alter
• alter command is used for alteration of table
structures. There are various uses
of alter command, such as,
• to add a column to an existing table
• To rename any existing column
• To change the datatype of any column or to modify
its size.
• alter is also used to drop a column.
• To Add Column to existing Table
Using alter command we can add a column to an
existing table.
alter table table-name add(column-name datatype);
alter table STUDENTS add(address char);

• The above command will add a new


column address to the Student table
• To Add Multiple Column to existing Table
Using alter command we can even add multiple
columns to an existing table.
Syntax:

alter table table-name add(column-name1


datatype1, column-name2 datatype2, column-name3
datatype3);
Example:

alter table STUDENTS add(father_name varchar(60),


mother_name varchar(60), dob date);

• The above command will add three new columns to


the Student table
• To Modify an existing Column
• alter command is used to modify data type of an
existing column .
alter table table-name modify(column-name
datatype);
• alter table Student modify(address varchar(30));
The above command will modify address column of
the Student table
• To Rename a column
• Using alter command you can rename an existing
column.
alter table table-name rename column old-column-
name to column-name;
alter table Student rename column address to
Location;
• To Drop a Column
• alter command is also used to drop columns.
alter table table-name drop(column-name);

alter table Student drop(address);


Location
• The above command will drop address column from
the Student table
example

CREATE TABLE STUDENTS ( ID number(10) NOT


NULL, NAME varchar2(20) NOT NULL, AGE
number(5) NOT NULL, ADDRESS varchar2 (25) );
• It will get that no null value is taken;
Truncate command
• truncate command removes all records from a
table. But this command will not destroy the table's
structure.
truncate table table-name
Example:
truncate table Student;
• The above query will delete all the records
of Student table.
• truncate command is different
from delete command.

delete command will delete all the rows from a table


whereas truncate command re-initializes a table(like a
newly created table).
RENAME query

• rename command is used to rename a table.


• rename old-table-name to new-table-name;
rename Student to Student_record;
• The above query will rename Student table
to Student_record.
DROP command
• drop query completely removes a table from database. This
command will also destroy the table structure. Following is its
Syntax,
• drop table table-name;
• EG:drop table Student;
• The above query will delete the Student table completely.
• It can also be used on Databases. For Example, to drop a
database,
• drop database Test;The above query will drop a database
named Test from the system.

SELECT DISTINCT
• The SELECT DISTINCT statement is used to return only
distinct (different) values.
• Inside a table, a column often contains many duplicate values;
and sometimes you only want to list the different (distinct)
values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;

SELECT DISTINCT Name FROM STUDENTS;


PRACTICE

• create table ‘YOUR NAME’ with attributes


name,reg_no,section and phone_number.
• 1.Add two column in the existing table with
Address and Percentage.
• 2.Change the Reg_No name to ID.
• 3.delete the column address
• 4.rename the table.
• 5.drop the table.
SQL Create Constraints
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.

Constraints can be column level or table level. Column level


constraints apply to a column, and table level constraints apply to
the whole table.
SQL Constraints
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value

UNIQUE - Ensures that all values in a column are different

PRIMARY KEY - A combination of a NOT NULL and


UNIQUE. Uniquely identifies each row in a table
SQL Constraints
The following constraints are commonly used in SQL:
FOREIGN KEY - Prevents actions that would destroy links
between tables
CHECK - Ensures that the values in a column satisfy a specific
condition
DEFAULT - Sets a default value for a column if no value is
specified
SQL NOT NULL Constraint

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Age int
);
SQL Primary Key Constraint
The PRIMARY KEY constraint uniquely identifies each
record in a table.

Primary keys must contain UNIQUE values, and cannot


contain NULL values.

A table can have only ONE primary key;


SQL Primary Key Constraint

CREATE TABLE Persons (


ID int,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Practice: example

Customers (CustomerID, CustomerName, Address,


City, PostalCode, Country );

insert into customers values (101, 'Name1',


'Address1', 'City1', 11111, 'India')
Practice: example

create table Customers (CustomerID int,


CustomerName varchar(150), Address
varchar(250), City varchar(200), PostalCode int,
Country varchar(200));

insert into customers values (101, 'Name1',


'Address1', 'City1', 11111, 'India')
• In SQL, which of the following is a data definition
language commands?
• A.RENAME
• B.REVOKE
• C.GRANT
• D.UPDATE
SQL WHERE Clause

• SELECT column1, column2, ...


FROM table_name
WHERE condition;

Example
SELECT * FROM STUDENTS
WHERE name=‘Rohan’;
SQL AND, OR and NOT Operators
1. The AND and OR operators are used to filter records based
on more than one condition.
2. The AND operator displays a record if all the conditions
separated by AND are TRUE.
3. The OR operator displays a record if any of the conditions
separated by OR is TRUE.
4. The NOT operator displays a record if the condition(s) is
NOT TRUE.
SQL AND, OR and NOT Operators
AND Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SQL AND, OR and NOT Operators
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
SQL AND, OR and NOT Operators

AND Example
select all fields from "Customers" where country
is “India" AND city is “City2":

SELECT * FROM Customers


WHERE Country=‘India' AND City=‘City2';
SQL AND, OR and NOT Operators
OR Example
select all fields from "Customers" where city is “City1" OR
“City2":
SELECT * FROM Customers WHERE City=‘City1' OR
City=‘City2';
SQL AND, OR and NOT Operators
NOT Example
select all fields from "Customers" where country is NOT
“India":
SELECT * FROM Customers WHERE NOT Country=‘India';
SQL AND, OR and NOT Operators
Combining AND, OR and NOT
select all fields from "Customers" where country is “India"
AND city must be “City2" OR “City3"
(use parenthesis to form complex expressions)
SELECT * FROM Customers WHERE Country=‘India' AND
(City=‘City2' OR City=‘City3’);
SQL AND, OR and NOT Operators
Combining AND, OR and NOT
select all fields from "Customers" where country is NOT
“India" and NOT “USA":

SELECT * FROM Customers WHERE NOT Country=‘India' AND


NOT Country='USA';
SQL ORDER BY
The ORDER BY keyword is used to sort the result-set in
ascending or descending order.
The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use the
DESC keyword.
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SQL ORDER BY
select all customers from the "Customers" table, sorted by the
"Country" column

SELECT * FROM Customers ORDER BY Country;


ORDER BY Several Columns
select all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column.

This means that it orders by Country, but if some rows have


the same Country, it orders them by CustomerName

SELECT * FROM Customers ORDER BY Country,


CustomerName;
ORDER BY Several Columns
select all customers from the "Customers" table, sorted
ascending by the "Country" and descending by the
"CustomerName" column

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;
Test for NULL Values
It is not possible to test for NULL values with comparison
operators.
use the IS NULL and IS NOT NULL operators instead

lists all customers with a NULL value in the "Address" field:


SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
Test for NULL Values
It is not possible to test for NULL values with comparison
operators.
use the IS NULL and IS NOT NULL operators instead

lists all customers with a NULL value in the "Address" field:


SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
Test for NULL Values
IS NOT NULL:

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are


different.
Both the UNIQUE and PRIMARY KEY constraints provide a
guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE
constraint.
However, you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
SQL UNIQUE Constraint

CREATE TABLE Persons ( ID int NOT NULL UNIQUE,


LastName varchar(255) NOT NULL, FirstName varchar(255),
Age int );

ALTER TABLE Persons ADD UNIQUE (ID);


SQL UNIQUE Constraint

CREATE TABLE Persons ( ID int NOT NULL UNIQUE,


LastName varchar(255) NOT NULL, FirstName varchar(255),
Age int );

ALTER TABLE Persons ADD UNIQUE (ID);


SQL CHECK Constraint

• The CHECK constraint is used to limit the value range that


can be placed in a column.
• If you define a CHECK constraint on a column it will allow
only certain values for this column.
• If you define a CHECK constraint on a table it can limit the
values in certain columns based on values in other columns
in the row.
SQL CHECK Constraint

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);

ALTER TABLE Persons ADD CHECK (Age>=18);


SQL CHECK Constraint

Now Insert into the table with age values <18


SQL QUERIES-2

DML-COMMANDS
• Data Manipulation Language (DML) statements are
used for managing data in database. DML
commands are not auto-committed. It means
changes made by DML command are not
permanent to database, it can be rolled back.
• SQL INSERT statement is a SQL query. It is used to
insert a single or a multiple records in a table.
• There are two ways to insert data in a table:
• By SQL insert into statement
– By specifying column names
– Without specifying column names.
• By SQL insert into select statement
1.SQL INSERT
Inserting data directly into a table

• If you specify the column names, syntax of the


insert into statement will be as follows:
• INSERT INTO TABLE_NAME (col1, col2, col3,.... col
N) VALUES (value1, value2, value 3, .... Value N);
• Here col1, col2, col3, .... colN are the columns of the
table in which you want to insert data.
Insert into statement without column
name
• INSERT into table-name values(data1,data2,..);
• Lets see an example,
• Consider a table Student with following fields.
• S_id,S_Name,age
• INSERT into Student values(101,'Adam',15);
• The above command will insert a record
into Student table.
Inserting data through SELECT
Statement
Create another table:
C (ID, Name, Address, City, PinCode, Country)
Inserting data through SELECT
Statement
Syntax
INSERT INTO table_name [(column1, column2, .... column)]
SELECT column1, column2, .... Column N FROM table_na
me [WHERE condition];

• Note: when you add a new row, you should make


sure that data type of the value and the column
should be matched.
Inserting data through SELECT
Statement

insert into C select CustomerID, CustomerName,


Address, City, PostalCode, Country from Customers
where CustomerID >=1
2.SQL SELECT
SQL SELECT
• The most commonly used SQL command is SELECT statement.
It is used to query the database and retrieve selected data that
follow the conditions we want.
• In simple words, we can say that the select statement used to
query or retrieve data from a table in the database.
• Let's see the syntax of select statement.
SELECT expressions FROM tables WHERE conditions;
Eg: select * from student where regno=1101
Optional clauses in SELECT statement
• There are some optional clauses in SELECT statement:
• [WHERE Clause] : It specifies which rows to retrieve.
• [ORDER BY Clause] : It specifies an order in which to return
the rows
• [GROUP BY Clause] : Groups rows that share a property so
that the aggregate function can be applied to each group.
• [HAVING Clause] : It selects among the groups defined by the
GROUP BY clause.
FILTERING TABEL DATA
• To view the complete table syntax is:
• Select * from tablename;
• Eg: select * from student; (* is known as wildcard)
• Three ways of filtering table data are:
A)Selected columns and all rows
B)Selected rows and all columns
C)Selected rows and selected columns.
1.SELECTED COLUMS AND ALL ROWS

• Syntax:
• SELECT <column1>,<column2> from <tablename>;
• Eg:

select age,id from student;


2.Selected Rows and All Columns

• The select statement used untill now display all


rows.
• Oracle provides the option of using a WHERE Clause
in an sql query to apply filter on the rows reterived.
• Syntax:
• SELECT *from<tablename> WHERE<condition>;
Select * from student where name=‘a’;
SQL WHERE
A WHERE clause in SQL is a data manipulation language
statement.
• WHERE clauses are not mandatory clauses of SQL DML
statements. But it can be used to limit the number of rows
affected by a SQL DML statement or returned by a query.
• Actually it filters the records. It returns only those queries
which fulfill the specific conditions.
• WHERE clause is used in SELECT, UPDATE, DELETE statement
etc.
• Let's see the syntax for sql where:
• SELECT column1, column 2, ... column n FROM table_name WHER
E [conditions]

• WHERE clause uses some conditional selection


= equal

> greater than

< less than

>= greater than or equal

<= less than or equal

<> not equal to


SQL AND
• The SQL AND condition is used in SQL query to
create two or more conditions to be met.
• It is used in SQL SELECT, INSERT, UPDATE and
DELETE statements.Let's see the syntax for SQL
AND:
SELECT columns FROM tables WHERE condition 1 AND condition 2;

• The SQL AND condition requires that both


conditions should be met.
• The SQL AND condition also can be used to join
multiple tables in a SQL statement.
3.SELECTED COLUMNS

• To view specific set of rows and columns the syntax


is:
• SELECT<column1><column2> from <table
name>where<condition>;
Select id,name from student where age=20;
SELECT DISTINCT
• The SQL DISTINCT command is used with SELECT key word
to retrieve only distinct or unique data.
• In a table, there may be a chance to exist a duplicate value
and sometimes we want to retrieve only unique values. In
such scenarios, SQL SELECT DISTINCT statement is used.
• Note: SQL SELECT UNIQUE and SQL SELECT DISTINCT
statements are same.
• Let's see the syntax of select distinct statement.
• SELECT DISTINCT column_name FROM table_name;
• Let's try to understand it by the table given below:

Student_Name Gender Mobile_Numbe HOME_TOWN


r
Rahul Ojha Male 7503896532 Lucknow
Disha Rai Female 9270568893 Varanasi
Sonoo Male 9990449935 Lucknow
Jaiswal

• Here is a table of students from where we want to retrieve distinct information For
example: distinct home-town.
• SELECT DISTINCT home_town FROM students
• Now, it will return two rows.

HOME_TOWN
Lucknow
Varanasi
Student_Name Gender Mobile_Numbe HOME_TOWN
r
Rahul Ojha Male 7503896532 Lucknow
Disha Rai Female 9270568893 Varanasi
Sonoo Male 9990449935 Lucknow
Jaiswal

CREATE TABLE Persons (


SName varchar(255),
Gender varchar(255),
Address varchar(255),
Mobile_no int;
);

SELECT DISTINCT home_town FROM students


• Now, it will return two rows.

HOME_TOWN
Lucknow
Varanasi
SQL SELECT COUNT
• The SQL COUNT() function is used to return the number of
rows in a query.
• The COUNT() function is used with SQL SELECT statement and
it is used to count the number of rows in a table having
enormous data.
• For example: If you have a record of the voters in a selected
area and want to count the number of voters then it is very
difficult to do it manually but you can do it easily by using the
SQL SELECT COUNT query.
Syntax:
• SELECT COUNT (expression) FROM tables WHERE cond
itions;

Example:
return the total number of names of customers.
SELECT COUNT(name) FROM Customers;
Now add one more row with Name kept blank:

Null fields will not be counted.


Syntax:
• SELECT COUNT (expression) FROM tables WHERE conditions;
Example:
SELECT COUNT(name) FROM Customers;
• It will return the total number of names of employee_table. But null
fields will not be counted.
SELECT COUNT(*) FROM employee_table;
• The "select count(*) from table" is used to return the number of records
in table.
SELECT COUNT(DISTINCT name) FROM employee_table;
• It will return the total distinct names of employee_table.
First add same name entries
3.SQL UPDATE
3.SQL UPDATE
• SQL UPDATE statement is used to change the data of the
records held by tables. Which rows is to be update, it is
decided by a condition. To specify condition, we use WHERE
clause.
• The UPDATE statement can be written in following form:
• UPDATE table_name SET [column_name1= value1,... colu
mn_nameN = valueN] [WHERE condition]

UPDATE customers SET name = 'b' WHERE customerid = 101


;
• Updating Multiple Fields:
• If you are going to update multiple fields, you
should separate each field assignment with a
comma.
• SQL UPDATE statement for multiple fields:
• UPDATE table_name SET field1 = new-
value1, field2 = new-value2, [WHERE CLAUSE]
4.SQL DELETE
4. SQL DELETE
• The SQL DELETE statement is used to delete rows
from a table. Generally DELETE statement removes
one or more records from a table.
• SQL DELETE Syntax
• DELETE FROM table_name [WHERE condition];
• Here table_name is the table which has to be
deleted. The WHERE clause in SQL DELETE
statement is optional here.
delete from table where customerid=101;
• But if you do not specify the WHERE condition it will
remove all the rows from the table.
• DELETE FROM table_name;
delete from student;
Difference between DELETE and TRUNCATE
statements
• There is a slight difference b/w delete and truncate statement.
The DELETE statement only deletes the rows from the table
based on the condition defined by WHERE clause or delete all
the rows from the table when condition is not specified.
• But it does not free the space containing by the table.
• The TRUNCATE statement: it is used to delete all the rows
from the table and free the containing space.
Difference between DROP and TRUNCATE

• When you use the drop statement it deletes the table's row
together with the table's definition so all the relationships of
that table with other tables will no longer be valid.
• When you drop a table:
• Table structure, its Relationship ,Integrity constraints and
Access privileges will also be dropped
• On the other hand when we TRUNCATE a table, the table
structure remains the same, so you will not face any of the
above problems.
SQL ROWNUM Clause

SELECT TOP number|percent column_name(s)


FROM table_name WHERE condition;

SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;

Length()
SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected


column.
The MAX() function returns the largest value of the selected
column.
SQL MIN() and MAX() Functions

MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;

MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL MIN() and MAX() Functions

SELECT MIN(CUSTOMERID) AS SmallestID FROM CUSTOMERS;

SELECT MAX(CUSTOMERID) AS LargestID


FROM CUSTOMERS;
SQL AVG() and SUM() Functions

AVG() Syntax
SELECT AVG(column_name) FROM table_name WHERE
condition;
The SUM() function returns the total sum of a numeric column.

SUM() Syntax
SELECT SUM(column_name) FROM table_name WHERE
condition;
SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search


for a specified pattern in a column.
There are two wildcards often used in conjunction with
the LIKE operator:

The percent sign (%) represents zero, one, or multiple


characters
The underscore sign (_) represents one, single character
SQL LIKE Operator

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';
SQL LIKE Operator
SQL LIKE Operator

1. select all customers with a CustomerName starting with "a"

SELECT * FROM Customers WHERE CustomerName LIKE 'a%';

2. select all customers with a CustomerName ending with "a"

SELECT * FROM Customers WHERE CustomerName LIKE '%a';

3. select all customers with a CustomerName that have "or" in any


position

SELECT * FROM Customers WHERE CustomerName LIKE


'%or%';
SQL LIKE Operator

4. select all customers with a CustomerName that have "r" in the


second position

SELECT * FROM Customers WHERE CustomerName LIKE '_r%';

5. select all customers with a CustomerName that starts with "a"


and are at least 3 characters in length:

SELECT * FROM Customers WHERE CustomerName LIKE


'a__%';
SQL LIKE Operator

6. select all customers with a ContactName that starts with "a" and
ends with "o":

SELECT * FROM Customers WHERE ContactName LIKE 'a%o';

7. select all customers with a CustomerName that does NOT start


with "a":

SELECT * FROM Customers WHERE CustomerName NOT LIKE


'a%';
SQL Aliases-
• SQL aliases are used to temporarily rename a table
or a column heading.Basically aliases are created to
make column names more readable.
• To give alias name to column
• SELECT column_name AS alias_nameFROM table_name;
• Eg:select regno as id from student;
• SQL Alias Syntax for Tables(to be done with joins)
• SELECT column_name(s)FROM table_name AS alias_name;
SQL ROWNUM Clause

SELECT TOP number|percent column_name(s)


FROM table_name WHERE condition;

SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;

Length()
SQL QUERIES-3

TCL and DCL-COMMANDS


DCL

Grant and revoke


DCL
1. Go to start of your windows and type sql
command line.
2. connect
Username: system
Password: same as that of workspace
Create a table (say customer) and insert few tuple
into it.
Select * from customer;
DCL
3. Create a user:
create user lpu identified by 123;
4. Connect as lpu
You will get session permission denied
Again connect as system and assign permissions
grant create session to lpu;
Now try login as lpu
DCL
3. Create a user:
create user lpu identified by 123;
4. Connect as lpu
You will get session permission denied
Again connect as system and assign permissions
grant create session to lpu;
Now try login as lpu
DCL
5. As user lpu
select * from system.customer;
Permission denied
grant select, insert on customer to lpu;
Again login ad lpu
And try to insert data into table or delete a tuple from
table
DCL
As system user:
6. grant delete on customer to lpu;
Again connect as lpu and check

7. revoke insert on tablename from username;


TCL
1.Commit command
• Commit command
• Commit command is used to permanently save any
transaaction into database.
• Following is Commit command's syntax,
• commit;
• You cant rollback if you commit;
2.Rollback command

• Rollback command
• This command restores the database to last
commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
• Following is Rollback command's syntax,
• rollback to savepoint-name;
3. Savepoint command

• savepoint command is used to temporarily save a


transaction so that you can rollback to that point
whenever necessary.
• Following is savepoint command's syntax,
• savepoint savepoint-name;
• Savepoint A;
Example of Savepoint and Rollback

• Following is the class table,


ID NAME
1 abhi
2 adam
4 alex

• Lets use some SQL queries on the above table and


see the results.
1. INSERT into class values(5,'Rahul');
2. commit;
3. UPDATE class set name='abhijit' where id='5';
savepoint A;
4. INSERT into class values(6,'Chris');
5. savepoint B;
6. INSERT into class values(7,'Bravo');
7. savepoint C; SELECT * from class;
• The resultant table will look like,
Now ID NAME

• rollback to B; 1 abhi
2 adam
• SELECT * from class; 4 alex

• rollback to A; 5 abhijit
6 chris
• SELECT * from class; 7 bravo

• You will see how data is displayed


TCL
1. Open sql command line
2. connect
Username: system
Password:
Create a table.
create table s(id int, name varchar(15));
Insert data into it.
insert into s values(101, ‘a’);
insert into s values(102, ‘b’);
select * from s;
TCL
3. rollback
4. select * from s;
Add another record
2. Save Point
A point up till which rollback is to be done
Create save point:
savepoint A;
insert into s values(103, ‘c’);
insert into s values(104, ‘d’);
savepoint B;
TCL
insert into s values(105, ‘e’);
insert into s values(106, ‘f’);
savepoint C;
rollback to B;

commit: permanent changes are saved. Rollback does not effect.


SQL QUERIES-3

TCL and DCL-COMMANDS


DCL

Grant and revoke


DCL
1. Go to start of your windows and type sql
command line.
2. connect
Username: system
Password: same as that of workspace
Create a table (say customer) and insert few tuple
into it.
Select * from customer;
DCL
3. Create a user:
create user lpu identified by 123;
4. Connect as lpu
You will get session permission denied
Again connect as system and assign permissions
grant create session to lpu;
Now try login as lpu
DCL
3. Create a user:
create user lpu identified by 123;
4. Connect as lpu
You will get session permission denied
Again connect as system and assign permissions
grant create session to lpu;
Now try login as lpu
DCL
5. As user lpu
select * from system.customer;
Permission denied
grant select, insert on customer to lpu;
Again login ad lpu
And try to insert data into table or delete a tuple from
table
DCL
As system user:
6. grant delete on customer to lpu;
Again connect as lpu and check

7. revoke insert on tablename from username;


TCL
1.Commit command
• Commit command
• Commit command is used to permanently save any
transaaction into database.
• Following is Commit command's syntax,
• commit;
• You cant rollback if you commit;
2.Rollback command

• Rollback command
• This command restores the database to last
commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
• Following is Rollback command's syntax,
• rollback to savepoint-name;
3. Savepoint command

• savepoint command is used to temporarily save a


transaction so that you can rollback to that point
whenever necessary.
• Following is savepoint command's syntax,
• savepoint savepoint-name;
• Savepoint A;
Example of Savepoint and Rollback

• Following is the class table,


ID NAME
1 abhi
2 adam
4 alex

• Lets use some SQL queries on the above table and


see the results.
1. INSERT into class values(5,'Rahul');
2. commit;
3. UPDATE class set name='abhijit' where id='5';
savepoint A;
4. INSERT into class values(6,'Chris');
5. savepoint B;
6. INSERT into class values(7,'Bravo');
7. savepoint C; SELECT * from class;
• The resultant table will look like,
Now ID NAME

• rollback to B; 1 abhi
2 adam
• SELECT * from class; 4 alex

• rollback to A; 5 abhijit
6 chris
• SELECT * from class; 7 bravo

• You will see how data is displayed


TCL
1. Open sql command line
2. connect
Username: system
Password:
Create a table.
create table s(id int, name varchar(15));
Insert data into it.
insert into s values(101, ‘a’);
insert into s values(102, ‘b’);
select * from s;
TCL
3. rollback
4. select * from s;
Add another record
2. Save Point
A point up till which rollback is to be done
Create save point:
savepoint A;
insert into s values(103, ‘c’);
insert into s values(104, ‘d’);
savepoint B;
TCL
insert into s values(105, ‘e’);
insert into s values(106, ‘f’);
savepoint C;
rollback to B;

commit: permanent changes are saved. Rollback does not effect.


DCL

Connect to database in sql CLI


connect :
system
pwd
1. Create user <un> identified by password;
2. Grant unlimited tablespace to <un>;
3. Grant create session to <un>;
grant create table to <un>;
1. connect
Username: <un>
password
DCL
Try to create table and insert data into it
5. Login as system
grant update on system.<tablename> to <un> with grant option
i.e. user u1 can grant to user u2
6.revoke create table from <un>;
7. revoke select on <tablename> from <un> cascade;
Revoke recursively
DCL
8. Create role:
create role <role_name> identified by password;
9. Grant select on <table name> to <role_name>;
10. Grant all on table to <role_name>;
11. Grant <role_name> to <username>
mcq
SQL QUERIES

CONSTRAINTS
• Sometimes the user wants to impose certain
restrictions on the data being stored in a table.
• These restrictions are called constraints.
• Constraints control the data being entered in a table.
• Once the data constraints are part of a table column
specification ,the oracle engine checks the data being
entered into that column against the data
constraints.
• If the data satisfy the constraint check,it is stored in
the column,otherwise the data is rejected.
• There are five types of constraints:
• Primary Key Constraint
• Unique Constraint
• Not Null Constraint
• Foreign Key Constraint
• Check Constraint
1.PRIMARY KEY CONSTRAINT
• Primary Key is applied to a column so that this
column can be used to uniquely identify each row
in the table.
• A table can have only one primary key.
• A primary key has following features:
• a) Primary key does not allow duplicate values.
• b)Primary Key does not allow null values
• Syntax:
• Columnname datatype(size) primary key
Example:
• Create table student(id number(10) primary
key,name varchar(10),city varchar(10));
or
• Create table student(id number(10),name
char(20),city varchar(10),primary key(id));
2.UNIQUE CONSTRAINT
• In a table there may be more than one column which forms the
candidate key.
• The unique constraint is used for the candidate key columns so
that they contain the unique values for different entities.
• Unique constraint will not allow the duplicate values in the
column to which they are attached.
• A unique key has following characteristics:
1. Unique key will not allow duplicate values.
2. Unique key can accept null value.
3. A table can have many unique constraints.
Syntax:
• Column datatype(size) unique

Example:
Create table student(id number(10) primary
key,name varchar(20) unique, city varchar(10));
3.NOT NULL CONSTRAINT
• Not null column constraint ensures that a table column cannot
be left empty.
• When a Not Null constraint is attached to a column then ,that
column becomes a mandatory column i.e. a value must be
entered into this column entire record will be rejected.
Syntax: columnname datatype(size) not null;
Eg: create table student(id number(10) not null);
5.FOREIGN KEY CONSTRAINT
• Foreign key is a column whose values are derived from the
primary key of other table.
• Foreign key enables us to establish the relationship between the
tables.
• The table in which the foreign key is defined is called as
foreign table or detail table
• The table whose primary key is referenced by the foreign table
is called as primary table or master table.
Foreign key has following characteristics:
1. Foreign key can contain only those values that are present in
the primary key of the master table
2. The values in the foreign key can be null or duplicate values.
3. In foreign key , if we insert a value that is not present in the
primary key of master table then that record will be rejected.
4. A record in the master table cannot be deleted if the
corresponding record in the foreign table exists.
Syntax: column_name datatype(size) references
master_table_name(column name)
Example: let department and course table as master and detail
tables
• Create table department(did number(10) primary key,
dname varchar(10));
After creating the master table, the detail table can be created as
following:
• Create table course(cid number(10) primary key, d_id
number(10) references department(did));
6.CHECK CONSTRAINT
• Check constraint is used to specify the data validation for a
column.
• Check constraint is a Boolean expression that is evaluated to
either true or false.
• Before inserting a value into the column(to which the check
constraint is attached),the oracle evaluates the Boolean
condition.
• If the condition is evaluated to true then the value is accepted
by the database.
• If the condition is evaluated to false then the value is rejected
by the database and an error message is displayed
• Syntax:
• Columnname datatype(size) check (boolean
expression)
• Eg:suppose that in the employee table the age of
the employee must be between 22 and 30
• Create table employee(emp_id char(10) primary
key,ename varchar(20),dept_name varchar(20)
check (dept_name in (‘cse’,’ece’,’me’)),age
number(10) check(age between 22 and 30));
• Q: Which of the following is not a class of constraint
in SQL Server?
a) NOT NULL
b) CHECK
c) NULL
d) UNIQUE
• Which of the constraint can be enforced one per
table?
a) Primary key constraint
b) Not Null constraint
c) Foreign Key constraint
d) Check constraint
• How can a SQL developer add a key on a table?
a) While creating a table
b) With Alter table command
c) both A and B
Key
• A key in DBMS is an attribute or a set of attributes that help to uniquely
identify a tuple (or row) in a relation (or table).
• Keys are also used to establish relationships between the different tables
and columns of a relational database.
• Individual values in a key are called key values.
Keys

• A key could either be a combination of more than one attribute (or columns) or
just a single attribute. The main motive of this is to give each record a unique
identity.
There are broadly seven types of keys in DBMS:
1.Super Key
2.Candidate Key
3.Primary Key
4.Composite Key
5.Alternative Key
6.Artificial Key
7.Foreign Key
Super Key

• Super Key is the set of all the keys which help to identify rows in a
table uniquely.
• This means that all those columns of a table that are capable of
identifying the other columns of that table uniquely will all be
considered super keys.
Super Key

• Super keys are those attributes of relation which have properties of


uniqueness.
Example: Consider
Student(roll number, name class)
Roll number has a property of uniqueness.
There are a number of other combinations:
a. roll number, name
b. roll number, class
c. roll number, name, class
Candidate Key

• Candidate keys are those attributes that uniquely identify rows of a


table.

• The Primary Key of a table is selected from one of the candidate keys.

• Candidate keys are defined as the set of fields from which the
primary key can be selected.
• It is an attribute or set of attributes that can act as a primary key for a
table to uniquely identify each record in that table.
Candidate Key

• A candidate key is a field or combination of fields that can act as a


primary key field for that table to uniquely identify each record in
that table.

• It can be defined as minimal Super Key or irreducible Super Key.

• In other words an attribute or a combination of attributes that


identifies the record uniquely but none of its proper subsets can
identify the records uniquely.
Candidate Key

Student(ID, Name, Address)


• For the above, we have only two Candidate Keys (i.e. Irreducible
Super Key) used to identify the records from the table uniquely.
1. ID can identify the record uniquely
2. combination of Name and Address can identify the record uniquely,
but neither Name nor Address can be used to identify the records
uniquely,
as it might be possible that we have two employees with similar name
or two employees from the same house.
Primary Key

• A primary key is a column of a table or a set of columns that helps to


identify every record present in that table uniquely.
• There can be only one primary Key in a table.
• Also, the primary Key cannot have the same values repeating for any
row.
• Every value of the primary key has to be different with no repetitions.
Example: Primary Key
Database designer can use one of the Candidate Key as a Primary Key. In
this case “ID” and “Name, Address” are Candidate Key, from which
consider “ID” Key as a Primary Key as the other key is the combination of
more than one attribute.
Alternate Key

• A table can have multiple choices for a primary key; however, it can
choose only one.
• So, all the keys which did not become the primary Key are called
alternate keys.
• (The candidate key which are not selected for primary key are known
as secondary keys or alternative keys)
• Alternate Key can be any of the Candidate Keys except for the Primary
Key.
• E.g. of Alternate Key is “Name, Address” as it is the only other
Candidate Key that is not a Primary Key.
Foreign Key

• Foreign Key is used to establish relationships between two tables.


• A foreign key will require each value in a column or set of columns to
match the Primary Key of the referential table.
Foreign Key

• Usually a foreign key in one table refers to the primary key (PK) of
another table. This way references can be made to link information
together and it is an essential part of database normalization
• The purpose of the foreign key is to ensure referential integrity of the
data i.e. only values that are supposed to appear in the database are
permitted
Foreign Key

Let consider
Department(Department_ID, Department_Name, Manager_ID,
Location_ID) with Department_ID as an Primary Key.
Employee (Emp_id, Emp_name, Department_ID)
can be defined as the Foreign Key as it can refer to the Department_ID
attribute of the Departments table (the referenced or parent table), a
Foreign Key value must match an existing value in the parent table or
be NULL.
Composite Key

• 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.
Composite Key

• If we use multiple attributes to create a Primary Key then that


Primary Key is called Composite Key (also called a Compound Key or
Concatenated Key).
• But any attribute that makes up the Composite key is not a simple
key in its own.
• E.g. of Composite Key, if we have used “Name, Address” as a Primary
Key then it will be our Composite Key.
Unique Key

• Unique Key is a column or set of columns that uniquely identify each


record in a table.
• All values will have to be unique in this Key.
• A unique Key differs from a primary key because it can have only one
null value, whereas a primary Key cannot have any null values.
SQL QUERIES-6

Aggregated Data Using the Group


Functions
What Are Group/AGGREGATE
Functions?
• Group functions operate on sets of rows to
give one result per group.
EMPLOYEES

Maximum salary in
EMPLOYEES table


Types of Group Functions
– AVG
– COUNT
– MAX
Group
– MIN functions
– SUM

SELECT column_name, aggregate_function(column_name) FROM


table_name WHERE column_name operator value GROUP BY
column_name;
SQL AGGREGATE FUNCTIONS

• SQL aggregate functions return a single value,


calculated from values in a column.Useful aggregate
functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
Demo Database
• Consider following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
• 1) AVG()
• Average returns average value after calculating
from values in a numeric column.
Its general Syntax is,
• SELECT AVG(column_name) from table_name;
• SQL query to find average of salary will be,
• SELECT avg(sal) from Emp;
SELECT round(avg(sal)) from emp
• 2) COUNT()
• Count returns the number of rows present in the
table either based on some condition or without
condition.
SELECT COUNT(column_name) from table-name;
• SELECT COUNT(ename) from Emp where sal >
2000;

• SELECT COUNT(distinct salary) from emp;


• 3) MAX()
• MAX function returns maximum value from selected
column of the table.
Syntax of MAX function is,
• SELECT MAX(column_name) from table-name;

SELECT MAX(sal) from emp;


4.MIN()
• MIN function returns minimum value from a
selected column of the table.

• SELECT MIN(column_name) from table-name;


• SELECT MIN(sal) from emp;
5.SUM()
• SUM function returns total sum of a selected
columns numeric values.

• SELECT SUM(column_name) from table-name;

• SQL query to find sum of salaries will be,


SELECT SUM(sal) from emp;
6. UPPER()
• SELECT UPPER(column_name) from table-name;

• SELECT UPPER(ename) from emp;


7.LOWER()
• SELECT LOWER(column_name) from table-name;

• SELECT LOWER(ename) from emp;


8. SUBSTR()
• SELECT substr(column_name, start, length) from
table-name;

• SQL query will be,


• select substr(ename,2,2) from emp;
9.ROUND()
• ROUND function is used to round a numeric field to
number of nearest integer. It is used on Decimal
point values. Syntax of Round function is,
• SELECT ROUND(column_name, decimals) from
table-name;
• SQL query is,
• SELECT ROUND(sal) from emp;
select round(avg(sal),3) from emp;
SQL QUERIES-8

Display Data from Multiple Tables:JOINS


Group by

• The SQL GROUP BY clause is used in collaboration with


the SELECT statement to arrange identical data into
groups. This GROUP BY clause follows the WHERE clause
in a SELECT statement
Syntax
• SELECT column1, column2 FROM table_name WHERE [
conditions ] GROUP BY column1, column2
• SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP
BY NAME;
Having clause

• The WHERE clause places conditions on the selected


columns, whereas the HAVING clause places conditions
on groups created by the GROUP BY clause.
• SELECT column1, column2 FROM table1, table2 WHERE [
conditions ] GROUP BY column1, column2 HAVING [
conditions ]
Example
E_id E_name Dept Salary
1 Ram HR 20000
2 Amit Finance 30000
3 Ravi HR 40000
4 Nitin IT 50000
5 Rahul Finance 60000

Condition for the group by:


1. Whatever attribute you write
with the group by, the same
you will write with select
2. If you want to use any other
attribute with select then apply
an aggregate function on that
Having clause

• SELECT id, SUM(salary) FROM employee GROUP BY id


having id>2;
Example
Write a query to display all E_id E_name Dept Salary
the department names with 1 Ram HR 20000
the number of employees 2 Amit Finance 30000
working in it. 3 Ravi HR 40000
select job from emp 4 Nitin IT 50000
group by dept; 5 Rahul Finance 60000

Condition for the group by:

select job, count(*) from 1. Whatever attribute you write


with the group by, the same
emp group by job; you will write with select
2. If you want to use any other
attribute with select then apply
select job, count(job) from an aggregate function on that
emp group by job;
JOINS
• SQL Join is used to fetch data from two or more tables, which
is joined to appear as single set of data.
• SQL Join is used for combining column from two or more
tables by using values common to both tables.
• Join Keyword is used in SQL queries for joining two or more
tables. Minimum required condition for joining table, is
(n-1) where n, is number of tables.
• A table can also join to itself known as, Self Join.
• (INNER) JOIN: Returns records that have matching values
in both tables
• LEFT (OUTER) JOIN: Returns all records from the left
table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right
table, and the matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a
match in either left or right table
JOINS
• This type of JOIN returns the cartesian product of
rows from the tables in Join.
• It will return a table which consists of records which
combines each row from the first table with each row
of the second table.
• Cross JOIN Syntax is:
• SELECT column-name-list from table-name1 CROSS
JOIN table-name2;
• SELECT * from class cross JOIN class_info;
Example of Cross JOIN

cust_details table
orders table
cid Cname addres oid oamount cid
s
1 Ram Delhi 1 55 1

2 Sham Chd 2 80 2

3 Ravi Chd 3 100 1

4 Amit Delhi 4 90 3
Inner Join
• Select records that has matching values in both tables
• Inner join will show common records

select * from cust_details, orders where cust_details.cid


= orders.cid
Using Join
select * from cust_details JOIN orders ON cust_details.cid
= orders.cid
Inner Join

Query:
Display cname, order amount of every customer (which
customer has placed order of what amount)?
select cid, cname, oamount from cust_details JOIN
orders ON cust_details.cid = orders.cid

Error: cid is ambigiuos


Inner Join
Query:
Display cname, order amount who has placed order of
what amount?
Sol:
select cust_details.cid, cname, oamount from
cust_details JOIN orders ON cust_details.cid = orders.cid
Left Outer Join
select cname, email from cust_details left join orders on
cust_details.cid = orders.cid

Query:
Find all the customers with their order details.
select cust_details.cid, cname, oamount from
cust_details LEFT JOIN orders ON cust_details.cid =
orders.cid
Right Outer Join
select cname, email from cust_details right join orders on
cust_details.cid = orders.cid

Query:
Find all the orders with their customer details.
select cust_details.cid, cname, oamount from
cust_details RIGHT JOIN orders ON cust_details.cid =
orders.cid
1.Cross JOIN or Cartesian Product
• This type of JOIN returns the cartesian product of
rows from the tables in Join.
• It will return a table which consists of records which
combines each row from the first table with each row
of the second table.
• Cross JOIN Syntax is:
• SELECT column-name-list from table-name1 CROSS
JOIN table-name2;
• SELECT * from class cross JOIN class_info;
Example of Cross JOIN

The Class table The class_info table

ID NAME ID Address

1 abhi 1 DELHI

2 adam 2 MUMBAI

4 alex 3 CHENNAI
Cross JOIN query will be,
• SELECT * from class cross JOIN class_info;
2.INNER Join or EQUI Join
• This is a simple JOIN in which the result is based on matched
data as per the equality condition specified in the query.
Inner Join Syntax is,
• SELECT <column-name-list> from <table-name1> INNER JOIN
<table-name2> WHERE <table-name1.column-name> =
<table-name2.column-name>;
• SELECT * from class, class_info where class.id = class_info.id;
• The result table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
3.NATURAL JOIN
• Natural Join is a type of Inner join which is based on
column having same name and same datatype
present in both the tables to be joined.
Natural Join Syntax is:
SELECT * from table-name1 NATURAL JOIN table-
name2;
• SELECT * from class NATURAL JOIN class_info;
Example of Natural JOIN

The class table, The class_info table,


ID NAME ID Address

1 abhi
1 DELHI

2 adam
2 MUMBAI
3 alex

3 CHENNAI
4 anu
• Natural join query will be,
• SELECT * from class NATURAL JOIN class_info;
• The result table will look like,
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI

• In the above example, both the tables being joined have ID


column(same name and same datatype), hence the records for
which value of ID matches in both the tables will be the result
of Natural Join of these two tables.
4.Outer JOIN
• Outer Join is based on both matched and
unmatched data. Outer Joins subdivide further into,
• Left Outer Join
• Right Outer Join
• Full Outer Join
Left Outer Join
• The left outer join returns a result table with
the matched data of two tables then remaining rows
of the left table and null for the right table's column.
• Left outer Join Syntax for Oracle is,
• select column-name-list from table-name1 LEFT
OUTER JOIN on table-name1.column-name = table-
name2.column-name.
• SELECT * FROM class LEFT OUTER JOIN class_info ON
(class.id=class_info.id);
Right Outer Join
• The right outer join returns a result table with
the matched data of two tables then remaining
rows of the right table and null for the left table's
columns.
• Right outer Join Syntax for Oracle is,
• select column-name-list from table-name1, table-
name2 on table-name1.column-name= table-
name2.column-name;
• SELECT * FROM class RIGHT OUTER JOIN class_info
ON (class.id=class_info.id);
Full Outer Join

• The full outer join returns a result table with


the matched data of two table then remaining rows
of both lefttable and then the right table.
• Full Outer Join Syntax is,
• select column-name-list from table-name1 FULL
OUTER JOIN table-name2 on table-name1.column-
name = table-name2.column-name;
• SELECT * FROM class FULL OUTER JOIN class_info
ON (class.id=class_info.id);
• The SQL SELF JOIN is used to join a table to itself as
if the table were two tables.
• SELECT a.column_name, b.column_name FROM
table1 a, table1 b WHERE a.common_field =
b.common_field;
• SELECT a.ID, b.NAME, a.SALARY FROM CUSTOMERS
a, CUSTOMERS b WHERE a.SALARY < b.SALARY;
PRACTICE

• Consider the following database:


• Employee(emp_id,name,dept_id,job,email,mobile_
number,join_date,salary,mgr_id)
• Department(dept_id,dept_name,block_no);
• Query to display the name , department number
and department name for all employees.
• Query to display the employee name and
department name for all the employees who have
an ‘a’ in there name
• Query to display name,job,department number and
department name for all the employees who work
in block no 9.
• Query to display the employee name and
department number of all the employees who work
in the same department as any given employee
• Query to display the name and joining date for all
employees who have joined before managers along
with their manger number and joining date
• Query to display each department number ,number
of employees in the department and the average
salary for all the employees in that department.
• Query to display the name and joining date of all
employees who had joined after employee ‘RAJ’.
solution
• Select name,employee.dept_id,dept_name from
employee,department where
employee.dept_id=department.dept_id;
• 2.select name,dept_name from
employee,department where
employee.dept_id=department.dept_id and name
like ‘%a%’;
• 3.select name,job,employee.dept_id,dept_name
from employee,department where
emplyee.dept_id=department.dept_id and
block_no=9
• 4.select name,dept_id from employee where dept_id=(select
dept_id from emplyee where name=‘&name’);
• 5.select a.name,a.join_date,a.mgr_id,b.join_date from
employee a,employee b where a.mgr_id=b.emp_id;
• 6.select employee.dept_id,count(name),avg(salary) from
department,employee where
department.dept_id=employee.dept_id group by
employee.dept_id;
• Select name,joindate from emplyee where join date>(select
join date from employee where name=‘RAJ’);
SQL QUERIES-6

Aggregated Data Using the Group


Functions
What Are Group/AGGREGATE
Functions?
• Group functions operate on sets of rows to
give one result per group.
EMPLOYEES

Maximum salary in
EMPLOYEES table


Types of Group Functions
– AVG
– COUNT
– MAX
Group
– MIN functions
– SUM

SELECT column_name, aggregate_function(column_name) FROM


table_name WHERE column_name operator value GROUP BY
column_name;
SQL AGGREGATE FUNCTIONS

• SQL aggregate functions return a single value,


calculated from values in a column.Useful aggregate
functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
Demo Database
• Consider following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
• 1) AVG()
• Average returns average value after calculating
from values in a numeric column.
Its general Syntax is,
• SELECT AVG(column_name) from table_name;
• SQL query to find average of salary will be,
• SELECT avg(sal) from Emp;
SELECT round(avg(sal)) from emp
• 2) COUNT()
• Count returns the number of rows present in the
table either based on some condition or without
condition.
SELECT COUNT(column_name) from table-name;
• SELECT COUNT(ename) from Emp where sal >
2000;

• SELECT COUNT(distinct salary) from emp;


• 3) MAX()
• MAX function returns maximum value from selected
column of the table.
Syntax of MAX function is,
• SELECT MAX(column_name) from table-name;

SELECT MAX(sal) from emp;


4.MIN()
• MIN function returns minimum value from a
selected column of the table.

• SELECT MIN(column_name) from table-name;


• SELECT MIN(sal) from emp;
5.SUM()
• SUM function returns total sum of a selected
columns numeric values.

• SELECT SUM(column_name) from table-name;

• SQL query to find sum of salaries will be,


SELECT SUM(sal) from emp;
6. UPPER()
• SELECT UPPER(column_name) from table-name;

• SELECT UPPER(ename) from emp;


7.LOWER()
• SELECT LOWER(column_name) from table-name;

• SELECT LOWER(ename) from emp;


8. SUBSTR()
• SELECT substr(column_name, start, length) from
table-name;

• SQL query will be,


• select substr(ename,2,2) from emp;
9.ROUND()
• ROUND function is used to round a numeric field to
number of nearest integer. It is used on Decimal
point values. Syntax of Round function is,
• SELECT ROUND(column_name, decimals) from
table-name;
• SQL query is,
• SELECT ROUND(sal) from emp;
select round(avg(sal),3) from emp;
VIEWS and
Subqueries
What is a View
• In SQL, a view is a virtual table based on the result-set of an SQL
statement.
• It is called virtual table because a view doesn’t store data, you can
query a view like you can a table.
• A view can combine data from two or more table, using joins, and just
contain a subset of information.
• This makes them convenient to abstract, or hide, complicated
queries.
• Create a view
Operations • Update a view
on View • Drop a view
Create a view
Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
create view s as select ROLL_NO, S_NAME from s_details;

Note: A view always shows up-to-date data! The database engine


recreates the data, using the view's SQL statement, every time a user
queries a view.
How to check the view
Syntax
SELECT * FROM viewname
Example
select * from n;
Updating a View
A view can be updated with the CREATE OR REPLACE VIEW command.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
create or replace view s as select ROLL_NO, S_NAME from s_details
where marks>80
update s set marks=80 where roll_no=1;
Dropping a view
• A view is deleted with the DROP VIEW command.
Syntax
DROP VIEW view_name;
Example
DROP VIEW s;
Benefits of a View
• There are many benefits to using views. Some of them are listed
below
• Enforce Business Rules
• Consistency
• Security
• Simplicity
• Space
Disadvantages of View
• Performance – What may seem like a simple query against a view
could turn out to be a hugely complex job for the database
engine. That is because each time a view is referenced, the query
used to define it, is rerun.
• Modifications – Not all views support INSERT, UPDATE,
or DELETE operations. In general, in order to support these
operations, the primary key and required fields must be present in
the view. Complex multi-table views are generally read-only.
Subqueries/Nested Queries
• Subqueries (also known as inner queries or nested queries) are used for
performing operations in multiple steps.
• A subquery is also called an inner query or inner select, while the
statement containing a subquery is also called an outer query or outer
select.
• The inner query executes first before its parent query so that the results of
an inner query can be passed to the outer query.
• A subquery may occur in :
• A SELECT clause
• A FROM clause
• A WHERE clause
Important Rules
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE
statements along with expression operator. It could be equality
operator or comparison operator such as =, >, =, <= and Like operator.
• A subquery is a query within another query. The outer query is called
as main query and inner query is called as subquery.
• The inner query executes first before its parent query so that the
results of an inner query can be passed to the outer query.
• Subquery must be enclosed in parentheses.
• Subqueries are on the right side of the comparison operator.
Syntax

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
example

SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Example
Stu_marks(stu_ID, marks)
Stu_details(student_ID, Name)
Example

SELECT * FROM Stu_marks WHERE stu_id = 300;

SELECT a.student_id, name, marks FROM


Stu_details a, Stu_marks b WHERE a.student_id =
b.stu_id AND marks >80;
Combining the queries
• Two queries identified students who get the better number than the
student who's StudentID is 300.
SELECT a.student_id, name, marks FROM Stu_details a, Stu_marks b
WHERE a.student_id = b.stu_id AND marks >( select marks from
Stu_marks where stu_ID=300);
Questions
• If a subquery (inner query) returns a null value to the outer query, the
outer query will not return any rows
Question
• Can we use order by clause in the inner query
DCL
UNIT 3

By:
Navjot Kaur
System Programming Domain
Functional Dependency
Functional Dependency

The functional dependency is a relationship that exists


between two attributes. It typically exists between the
primary key and non-key attribute within a table.

X → Y
The left side of FD is known as a determinant, the right side of
the production is known as a dependent.
Functional Dependency
Assume we have an employee table with attributes: Emp_Id,
Emp_Name, Emp_Address.

Here, Emp_Id attribute can uniquely identify the Emp_Name


attribute of employee table because if we know the Emp_Id,
we can tell that employee name associated with it.
Functional dependency can be written as:
Emp_Id → Emp_Name
We can say that Emp_Name is functionally dependent on
Emp_Id.
Functional Dependency
• Functional Dependency (FD) determines the relation of one
attribute to another attribute in a database management
system (DBMS) system.
• Functional dependency helps you to maintain the quality of
data in the database.
• A functional dependency is denoted by an arrow →.
• The functional dependency of X on Y is represented by X → Y.
Functional Dependency
X→Y X Y
1 1
For a value of X the value of Y must be same
2 1
Everywhere. If X is same the Y must be same. 3 2
4 3
FD: X→Y
2 5
if t1.X = t2.X
then t1.Y = t2.Y
Fully Functional Dependency
X→Y
Y is Fully Functional Dependent on X if Y is Functional Dependent
on X but not functional dependent of any proper subset of X
Functional Dependency
X→Y Eid Enam Dept
e
Y is Fully Functional Dependent on X 101 A Mkt
102 B Fin
if Y is Functional Dependent on X 103 B HR
but not functional dependent of any proper
subset of X.
Eid,ename → dept
Subsets:

Eid→ dept
Ename→dept
Functional Dependency

• If we know the value of Employee number, we can obtain


Employee Name, city, salary, etc.
• By this, we can say that the city, Employee Name, and salary
are functionally depended on Employee number.
Functional Dependency Rules

• Three most important rules for Functional Dependency:


Types of Functional Dependency
1. Multivalued dependency
2. Trivial functional dependency
3. Non-trivial functional dependency
4. Transitive dependency
Types of Functional Dependency

1. Multivalued dependency
• Multivalued dependency occurs in the situation where there
are multiple independent multivalued attributes in a single
table.
• A multivalued dependency is a complete constraint between
two sets of attributes in a relation.
• It requires that certain tuples be present in a relation.
Types of Functional Dependency
1. Multivalued dependency

maf_year and color are independent of each other but dependent on


car_model. these two columns are said to be multivalue dependent on
car_model.
Types of Functional Dependency

1. Multivalued dependency

car_model -> maf_year


car_model-> colour
Trivial Dependency
Eid,ename → Eid Eid Enam Dept
e
101 A Mkt
102 B Fin
103 A HR
But CEO is not a subset of Company, and hence it's non-trivial functional
dependency.
Non Trivial Dependency
Eid,ename → dept Eid Enam Dept
e
101 A Mkt
102 B Fin
103 A HR
Types of Functional Dependency

1. Trivial functional dependency

A → B has trivial functional dependency if B is a subset of A.


The following dependencies are also trivial like: A → A, B → B
Types of Functional Dependency
Example:
Consider a table with two columns Employee_Id and
Employee_Name.
{Employee_id, Employee_Name} → Employee_Id is a trivial
functional dependency as
Employee_Id is a subset of {Employee_Id, Employee_Name}.

Also, Employee_Id → Employee_Id and Employee_Name →


Employee_Name are trivial dependencies too.
Types of Functional Dependency
2. Non-trivial functional dependency
A → B has a non-trivial functional dependency if B is not a
subset of A.
When A intersection B is NULL, then A → B is called as
complete non-trivial.
Example:
ID → Name,
Name → DOB
Closure

The set of all those attributes which can be functionally


determined from an attribute set is called as a closure of that
attribute set.

Closure of attribute set {X} is denoted as {X}+.


Closure
Step-01: Add the attributes contained in the attribute set for
which closure is being calculated to the result set.

Step-02:
Recursively add the attributes to the result set which can be
functionally determined from the attributes already contained
in the result set.
Closure helps to find all possible candidate keys in a
relation.
Closure: Example

1. R(ABCD)
FD {A→ B, B→ C, C→ D}
A +: means what A can determine
= {A B C D} A will determine itself because of reflexive rule
(self loop)

A can determine all attributes of R. (property of candidate key)


CK = {A}
Closure: Example
1. R(ABCD)
FD {A→ B, B→ C, C→ D}
B+ = { BCD}
B cant be CK because it does not det. A

C+ = {CD} not a CK
D+= {D} not a CK
Only A is Candidate Key
Closure: Example
1. R(ABCD)
FD {A→ B, B→ C, C→ D}
Prime Attribute: which is used in making the candidate key
Prime Attribute = {A}
Non Prime: {BCD}
Closure: Example
2. R(ABCD)
FD { A→B, B→C, C→D, D→A)
Closure: Example
3. R(A,B,C,D,E)
FD : {A→B, B→C, C→D, D→E}
Find closure of A,B,C,D,E, AD+, CD+
Closure
Example-

Consider a relation R ( A , B , C , D , E , F , G ) with the functional


dependencies-
A → BC
BC → DE
D→F
CF → G
Closure
Closure of attribute A-
A+ = { A }
= { A , B , C } ( Using A → BC )
= { A , B , C , D , E } ( Using BC → DE )
= { A , B , C , D , E , F } ( Using D → F )
= { A , B , C , D , E , F , G } ( Using CF → G )
Thus,
A+ = { A , B , C , D , E , F , G }
Closure
Closure of attribute D-
D+ = { D }
= { D , F } ( Using D → F )
We can not determine any other attribute using attributes D and F
contained in the result set.
Thus,
D+ = { D , F }
Closure
Closure of attribute set {B, C}-
{ B , C }+= { B , C }
= { B , C , D , E } ( Using BC → DE )
= { B , C , D , E , F } ( Using D → F )
= { B , C , D , E , F , G } ( Using CF → G )
Thus,
{ B , C }+ = { B , C , D , E , F , G }
Normalization
What is it and what is need of normalization:
Process of organizing data in DB.
1. Minimize redundancy in relation
2. Divide big table into small tables
Need
To remove anomalies
Normalization
• If a database design is not perfect, it may contain
anomalies.
Problems Without Normalization
If a table is not properly normalized and have data redundancy
then it will not only consume extra memory space but will also
make it difficult to handle and update the database, without
facing data loss.

Insertion, Updation and Deletion Anomalies are very frequent if


database is not normalized.

To understand these anomalies let us take an example of a


Student table.
Problems Without Normalization

We have data of 4 Computer Sci. students.

As we can see, data for the fields branch, hod(Head of Department)


and office_tel is repeated for the students who are in the same
branch in the college, this is Data Redundancy.
Problems Without Normalization
Data Redundancy: Sid Name Age Branch Branch HOD
_code _name
Duplicate data in the table 1 A 18 101 CSE XYZ

Disadvantage: 2 B 19 101 CSE XYZ


3 C 18 101 CSE XYZ
1. Insertion, deletion and 4 D 21 102 ECE PQR
update Anomalies 5 E 20 102 ECE PQR
6 F 19 103 ME KLM
INSERTION ANOMALY:
When certain data attributes cant be inserted into the database
without the presence of other data.
In this table, a new branch civil cant be added until you have the sid
of the student who took admission in that branch civil. You cant
enter data without sid because entire branch data is kept in a single
table.
Anomalies
1. Update anomalies − If data items are scattered and are not
linked to each other properly, then it could lead to strange
situations.

For example, when we try to update one data item having its
copies scattered over several places, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
2. Deletion anomalies − We tried to delete a record that is
unwanted but it leads to deletion of the data that us wanted.
Example: we wanted to delete details of student but did not
want to delete branch information. Now because branch info.
Is not stored separately so it will also be deleted.

3. Insert anomalies − We tried to insert data in a record that


does not exist at all.
Anomalies

Insertion anomaly:

If a tuple is inserted in referencing relation and referencing attribute value is not


present in referenced attribute, it will not allow inserting in referencing relation.

For Example, If we try to insert a record in STUDENT_COURSE with STUD_NO =7, it


will not allow.
Anomalies

Deletion and Updation anomaly: If a tuple is deleted or updated from referenced


relation and referenced attribute value is used by referencing attribute in
referencing relation, it will not allow deleting the tuple from referenced relation.

For Example, If we try to delete a record from STUDENT with STUD_NO =1.
Anomalies
1. Update anomalies − If data items are scattered and are not
linked to each other properly, then it could lead to strange
situations.

For example, when we try to update one data item having its
copies scattered over several places, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
2. Deletion anomalies − We tried to delete a record, but parts
of it was left undeleted because of unawareness, the data is
also saved somewhere else.

3. Insert anomalies − We tried to insert data in a record that


does not exist at all.
Anomalies

Insertion anomaly:

If a tuple is inserted in referencing relation and referencing attribute value is not


present in referenced attribute, it will not allow inserting in referencing relation.

For Example, If we try to insert a record in STUDENT_COURSE with STUD_NO =7, it


will not allow.
Anomalies

Deletion and Updation anomaly: If a tuple is deleted or updated from referenced


relation and referenced attribute value is used by referencing attribute in
referencing relation, it will not allow deleting the tuple from referenced relation.

For Example, If we try to delete a record from STUDENT with STUD_NO =1.
Closure
Step-01: Add the attributes contained in the attribute set for
which closure is being calculated to the result set.

Step-02:
Recursively add the attributes to the result set which can be
functionally determined from the attributes already contained
in the result set.
Closure
Example-
Consider a relation R ( A , B , C , D , E , F , G ) with the functional
dependencies-
A → BC
BC → DE
D→F
CF → G
Closure
Closure of attribute A-
A+ = { A }
= { A , B , C } ( Using A → BC )
= { A , B , C , D , E } ( Using BC → DE )
= { A , B , C , D , E , F } ( Using D → F )
= { A , B , C , D , E , F , G } ( Using CF → G )
Thus,
A+ = { A , B , C , D , E , F , G }
Closure
Closure of attribute D-
D+ = { D }
= { D , F } ( Using D → F )
We can not determine any other attribute using attributes D and F
contained in the result set.
Thus,
D+ = { D , F }
Closure
Closure of attribute set {B, C}-
{ B , C }+= { B , C }
= { B , C , D , E } ( Using BC → DE )
= { B , C , D , E , F } ( Using D → F )
= { B , C , D , E , F , G } ( Using CF → G )
Thus,
{ B , C }+ = { B , C , D , E , F , G }
Functional Dependency
Functional Dependency

The functional dependency is a relationship that exists


between two attributes. It typically exists between the
primary key and non-key attribute within a table.

X → Y
The left side of FD is known as a determinant, the right side of
the production is known as a dependent.
Functional Dependency
Assume we have an employee table with attributes: Emp_Id,
Emp_Name, Emp_Address.

Here, Emp_Id attribute can uniquely identify the Emp_Name


attribute of employee table because if we know the Emp_Id,
we can tell that employee name associated with it.
Functional dependency can be written as:
Emp_Id → Emp_Name
We can say that Emp_Name is functionally dependent on
Emp_Id.
Functional Dependency
• Functional Dependency (FD) determines the relation of one
attribute to another attribute in a database management
system (DBMS) system.
• Functional dependency helps you to maintain the quality of
data in the database.
• A functional dependency is denoted by an arrow →.
• The functional dependency of X on Y is represented by X → Y.
Functional Dependency

• If we know the value of Employee number, we can obtain


Employee Name, city, salary, etc.
• By this, we can say that the city, Employee Name, and salary
are functionally depended on Employee number.
Functional Dependency Rules

• Three most important rules for Functional Dependency:


Types of Functional Dependency
1. Multivalued dependency
2. Trivial functional dependency
3. Non-trivial functional dependency
4. Transitive dependency
Types of Functional Dependency
1. Multivalued dependency
• Multivalued dependency occurs in the situation where there
are multiple independent multivalued attributes in a single
table.
• A multivalued dependency is a complete constraint between
two sets of attributes in a relation.
• It requires that certain tuples be present in a relation.
Types of Functional Dependency
1. Multivalued dependency

maf_year and color are independent of each other but dependent on


car_model. these two columns are said to be multivalue dependent on
car_model.
Types of Functional Dependency
1. Multivalued dependency

car_model -> maf_year


car_model-> colour
But CEO is not a subset of Company, and hence it's non-trivial functional
dependency.
Types of Functional Dependency
1. Trivial functional dependency

A → B has trivial functional dependency if B is a subset of A.


The following dependencies are also trivial like: A → A, B → B
Types of Functional Dependency
Example:
Consider a table with two columns Employee_Id and
Employee_Name.
{Employee_id, Employee_Name} → Employee_Id is a trivial
functional dependency as
Employee_Id is a subset of {Employee_Id, Employee_Name}.

Also, Employee_Id → Employee_Id and Employee_Name →


Employee_Name are trivial dependencies too.
Types of Functional Dependency
2. Non-trivial functional dependency
A → B has a non-trivial functional dependency if B is not a
subset of A.
When A intersection B is NULL, then A → B is called as
complete non-trivial.
Example:
ID → Name,
Name → DOB
Fully Functional Dependency
X→Y
Y is Fully Functional Dependent on X if Y is Functional Dependent
on X but not functional dependent of any proper subset of X
Functional Dependency
X→Y Eid Enam Dept
e
Y is Fully Functional Dependent on X 101 A Mkt
102 B Fin
if Y is Functional Dependent on X 103 B HR
but not functional dependent of any proper
subset of X.
Eid,ename → dept
Subsets:

Eid→ dept
Ename→dept
Normalization
What is it and what is need of normalization:
Process of organizing data in DB.
1. Minimize redundancy in relation
2. Divide big table into small tables
Need
To remove anomolies
Normalization
• If a database design is not perfect, it may contain
anomalies, which are like a bad dream for any database
administrator. Managing a database with anomalies is next
to impossible.
Problems Without Normalization
If a table is not properly normalized and have data redundancy
then it will not only consume extra memory space but will also
make it difficult to handle and update the database, without
facing data loss.

Insertion, Updation and Deletion Anomalies are very frequent if


database is not normalized.

To understand these anomalies let us take an example of a


Student table.
Problems Without Normalization
Data Redundancy:
Duplicate data in the table
Disadvantage:
1. Insertion, deletion and
update Anomalies
Sid Name Age Branch Branch HOD
_code _name
1 A 18 101 CSE XYZ
2 B 19 101 CSE XYZ
3 C 18 101 CSE XYZ
4 D 21 102 ECE PQR
5 E 20 102 ECE PQR
6 F 19 103 ME KLM
Problems Without Normalization
Sid Name Age Branch Branch HOD
INSERTION ANOMALY: _code _name
1 A 18 101 CSE XYZ
When certain data
2 B 19 101 CSE XYZ
attributes cant be inserted 3 C 18 101 CSE XYZ

into the database without 4 D 21 102 ECE PQR


5 E 20 102 ECE PQR
the presence of other data.
6 F 19 103 ME KLM

In this table, a new branch civil cant be added until you have the sid
of the student who took admission in that branch civil. You cant
enter data without sid because entire branch data is kept in a single
table.
Anomalies
2. Update anomalies − If data items are scattered and are not
linked to each other properly, then it could lead to strange
situations.

For example, when we try to update one data item having its
copies scattered over several tables, a few instances get
updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Anomalies
3. Deletion anomalies − We tried to delete a record that is
unwanted but it leads to deletion of the data that we wanted.
Example: we wanted to delete details of student but did not
want to delete branch information. Now because branch info.
Is not stored separately so it will also be deleted.
Anomalies

Insertion anomaly:

If a tuple is inserted in referencing relation and referencing attribute value is not


present in referenced attribute, it will not allow inserting in referencing relation.

For Example, If we try to insert a record in STUDENT_COURSE with STUD_NO =7,


it will not allow.
Anomalies

Deletion and Updation anomaly: If a tuple is deleted or updated from referenced


relation and referenced attribute value is used by referencing attribute in
referencing relation, it will not allow deleting the tuple from referenced relation.

For Example, If we try to delete a record from STUDENT with STUD_NO =1.
Normalization
• Normalization is the process of organizing the data in the
database.
• Normalization is used to minimize the redundancy from a
relation or set of relations. It is also used to eliminate the
undesirable characteristics like Insertion, Update and Deletion
Anomalies.
• Normalization divides the larger table into the smaller table
and links them using relationship.
• The normal form is used to reduce redundancy from the
database table.
Normalization Types
Normalization Types
First Normal Form (1NF)

• A relation will be 1NF if it contains an atomic value.


• It states that an attribute of a table cannot hold multiple values. It
must hold only single-valued attribute.
• First normal form disallows the multi-valued attribute, composite
attribute, and their combinations.
Normalization Types: First Normal Form (1NF)
Normalization Types: First Normal Form (1NF)
Normalization Types: First Normal Form (1NF)
Second Normal Form (2NF)
• In the 2NF, relational must be in 1NF.
• In the second normal form, all non-key attributes
are fully functional dependent on the primary key
• There should not be partial dependency
Examples: check whether this Relation is in 2NF or
not?
R(ABCD) and FD:{AB→D , B→ C}
Sol:
ABCD: after reducing: (AB)+={ABCD} So AB is a CK.
Prime att: {AB} and Non prime:{CD}
Concept is: As AB is a CK this means {AB} pair cant be
empty, but individual values can be null.
A B

From B→C : partial dependency 1 -


- 2
If B is null you cant find C - -
3 4
Examples: check whether this Relation is in 2NF or
not?
R(ABCD) and FD:{AB→D , B→ C}
Example2: check whether this Relation is in 2NF or
not?

scoreid studen subject Marks Teache


tid id r

1 10 1 82 A
2 10 2 87 B
3 11 1 86 C
4 11 2 85 D
5 11 4 80 E
Example2: check whether this Relation is in 2NF or
not? scor stud subj Mar Teac
eid entid ectid ks her
1. Display teacher of student with sid=10
1 10 1 82 A
2. Display teacher for subjectid=1 2 10 2 87 B
3 11 1 86 C
(studentid,subjectid) → teacher
4 11 2 85 D
To know the teacher, subject id is enough. 5 11 4 80 E

subjectid → teacher {partial dependency}


Score table: scoreid studentid subjectid marks

Subject:
subjectid subjectname teacher
Final tables:
Student: studentid name regno branch address

Score table
scoreid studentid subjectid marks

subject
subjectid subjectname teacher
3NF: No transitive dependency
When a non prime attribute in a table depends upon
other non prime attribute.
Consider score table with additional 2 attributes:
scoreid studentid subjectid marks examname Total marks

In the score table PK is composite (studentid, subjectid)


CSE student will have graphics exam but Civil student
will not.
It means marks are dependent on exam
3NF: No transitive dependency

scoreid studentid subjectid marks examname Total marks

examname→ totalmarks {both these are non prime att.}

scoreid studentid subjectid marks

examid examname totalmarks


BCNF: 3.5NF
Table must be in 3NF and for any dependency A→ B
A should be a super key.

A→B
A: non prime and B: prime :{not allowed in BCNF}
BCNF: 3.5NF
studentid subject professor
101 Java P.Java
101 C P.C
102 Java P.Java
103 C++ P.C++
104 java P.Java

One student can enroll in more subjects and one


teacher is assigned 2 subjects
BCNF: 3.5NF
studenti subject professo
Multiple prof. teach same d r
101 Java P.Java
subject 101 C P.C

(subjectid,subject)→ professor 102 Java P.Java


103 C++ P.C++
professor → subject 104 java P.Java

Nonprime → Prime att.


studentid professor

professorid professor subject


4NF
It must be in BCNF and there must not be any
multivalued dependency.
A -->> B
4NF
A table should have at least 3 columns to have multi-
valued dependency.
Reason:

Distribute data in multiple rows will solve the problem,


no need to decompose the table.
4NF
A table should have at least 3 columns to have multi-
valued dependency.
Reason:
For a table with 3 values,
A→ B is a multivalued dependency then B and C should
be independent of each other.
4NF
There are 3 conditions for multi-valued dependency.
1. A→B for a single value of A, more than one value of
B exist
2. Table has atleast 3 columns
3. For a table with A, B, C attributes, B and C should be
independent of each other.
If all there are true, then table has multi valued
dependency.
4NF
Studentid Course Hobby
1 Science Cricket
1 Maths Hockey
2 C++ Cricket
3 Php Hockey

Studentid Course Hobby


Course_opted(sid,course)
1 Science Cricket
1 Science Hockey
1 Maths Cricket
1 Maths Hockey
Hobbies(sid,hobbies)
5NF: PJNF
Project Join Normal Form
There must be not join dependency.
If a relation has join dependency then it is divided into small
relations such that if we join back it given original relation.
5NF: PJNF
Supplier Product Customer

ACM EXCEL FORD


ROBO GEAR GM
ROBO SWITCH FORD
VAT TOOL BOX TATA
5NF: PJNF
1. One supplier can supply given part to one or more customers

Ternary relationship
5NF:
2. Customer buying from supplier and supplier can have one or
more products.
5NF:
3. Product used by customer can be supplied by one or more
suppliers
5NF
Binary Relationships can be:
Supplier-customer
Customer-product
Product-supplier
5NF
5NF: final tables
Supplier product customer

Supplier Product Supplier Customer Customer Product


To convert the given table into 2NF, we decompose it into two
tables:
To convert the given table into 2NF, we decompose it into two
tables:
Third Normal Form (3NF)
•A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
•3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
•If there is no transitive dependency for non-prime attributes, then the
relation must be in third normal form.
•A relation is in third normal form if it holds atleast one of the following
conditions for every non-trivial function dependency X → Y.
1.X is a super key.
2.Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Super key in the table above:

{EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....so on


Candidate key: {EMP_ID}

Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.

Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP


dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It violates the rule of third normal
form.

That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
Boyce Codd normal form (BCNF)

•BCNF is the advance version of 3NF. It is stricter than 3NF.

•A table is in BCNF if every functional dependency X → Y, X is the


super key of the table.

•For BCNF, the table should be in 3NF, and for every FD, LHS is super
key.
Candidate key: {EMP-ID, EMP-DEPT}
Fourth normal form (4NF)

•A relation will be in 4NF if it is in Boyce Codd normal form and has


no multi-valued dependency.

•For a dependency A → B, if for a single value of A, multiple values of


B exists, then the relation will be a multi-valued dependency.
Fourth normal form (4NF)

•A relation will be in 4NF if it is in Boyce Codd normal form and has


no multi-valued dependency.

•For a dependency A → B, if for a single value of A, multiple values of


B exists, then the relation will be a multi-valued dependency.
The given STUDENT table is in 3NF, but the COURSE and HOBBY are
two independent entity. Hence, there is no relationship between
COURSE and HOBBY.

In the STUDENT relation, a student with STU_ID, 21 contains two


courses, Computer and Math and two hobbies, Dancing and Singing.
So there is a Multi-valued dependency on STU_ID, which leads to
unnecessary repetition of data.

So to make the above table into 4NF, we can decompose it into two
tables:
Normalization

1. First Normal Form –


If a relation contain composite or multi-valued attribute, it
violates first normal form or a relation is in first normal form if it
does not contain any composite or multi-valued attribute.

A relation is in first normal form if every attribute in that relation


is singled valued attribute.
2. Second Normal Form –
• To be in second normal form, a relation must be in first normal
form and relation must not contain any partial dependency.
• A relation is in 2NF if it has No Partial Dependency, i.e., no
non-prime attribute (attributes which are not part of any
candidate key) is dependent on any proper subset of any
candidate key of the table.
• Partial Dependency – If the proper subset of candidate key
determines non-prime attribute, it is called partial
dependency.
2NF
2NF
Here,
COURSE_FEE cannot alone decide the value of COURSE_NO or STUD_NO;

COURSE_FEE together with STUD_NO cannot decide the value of COURSE_NO;

COURSE_FEE together with COURSE_NO cannot decide the value of STUD_NO;


Hence,

COURSE_FEE would be a non-prime attribute, as it does not belong to the one


only candidate key {STUD_NO, COURSE_NO} ;
2NF
But, COURSE_NO -> COURSE_FEE , i.e., COURSE_FEE is dependent on
COURSE_NO, which is a proper subset of the candidate key.

Non-prime attribute COURSE_FEE is dependent on a proper subset of the


candidate key, which is a partial dependency and so this relation is not in 2NF.

To convert the above relation to 2NF,


we need to split the table into two tables such as :
Table 1: STUD_NO, COURSE_NO
Table 2: COURSE_NO, COURSE_FEE
2NF
Practice: MCQ
1. Attributes are
(i) properties of relationship
(ii)attributed to entities
(iii)properties of members of an entity set
(a) i
(b) i and ii
(c) i and iii
(d) iii
Practice: MCQ
By relation cardinality we mean:
(a) number of items in a relationship
(b) number of relationships in which an entity can appear
(c) number of items in an entity
(d) number of entity sets which may be related to a given entity
Practice: MCQ
By relation cardinality we mean:
(a) number of items in a relationship
(b) number of relationships in which an entity can appear
(c) number of items in an entity
(d) number of entity sets which may be related to a given entity
Practice: MCQ
An attribute y may be functionally dependent on
(i) a composite attribute x,y
(ii)a single attribute x
(iii)no attribute
(a) i and ii
(b) i and iii
(c) ii and iii
(d) iii
Practice: MCQ
The following functional dependencies are given:
AB → CD, AF → D, DE → F, C → G, F → E, G
→A
Which one of the following options is false?
(a) {CF}+ = {ACDEFG}
(b) {BG}+ = {ABCDG}
(c) {AF}+ = {ACDEFG}
(d) {AB}+ = {ABCDFG}
Practice: MCQ
Display name of cities (all entries) whose temperature is in the
range 21 and 39
A. SELECT * FROM weather WHERE temperature NOT IN (21
to 39);
B. SELECT * FROM weather WHERE temperature NOT IN (21
and 39);
C. SELECT * FROM weather WHERE temperature NOT
BETWEEN 21 to 39;
D. SELECT * FROM weather WHERE temperature BETWEEN
21 AND 39;

You might also like