DBMS
DBMS
SRI
WOMEN’SCOLLEGE
(Affiliated to Periyar University)
Periyeri(village),Thalaivasal(Via)
Attur (Tk), Salem (Dt) -636112.
DEPARTMENT
OF
COMPUTERSCIENCE
DBMS
2025-2026
III CS
23UCS05
UNIT-I-DATABASE CONCEPT:
Database Concepts:Database Systems - Data vs Information - Introducing the database -File
system - Problems with file system – Database systems. Data models - Importance - Basic
Building Blocks - Business rules - Evolution of Data models - Degrees of Data Abstraction
UNIT-II-DESIGN CONCEPT
Design Concepts: Relational database model - logical view of data-keys –Integrity rules -
relational set operators - data dictionary and the system catalog - relationships -data redundancy
revisited -indexes - codd's rules. Entity relationship model - ER diagram
UNIT-IV-ADVANCED SQL:
Advanced SQL:Relational SET Operators: UNION – UNION ALL – INTERSECT - MINUS.SQL Join
Operators: Cross Join – Natural Join – Join USING Clause – JOIN ON Clause – Outer Join.Sub Queries
and Correlated Queries: WHERE – IN – HAVING – ANY and ALL – FROM. SQL Functions: Date and
Time Function – Numeric Function – String Function – Conversion
UNIT-V-PL/SQL
PL/SQL:A Programming Language: History – Fundamentals – Block Structure – Comments –
Data Types – Other Data Types – Variable Declaration – Assignment operation –Arithmetic
operators.Control Structures and Embedded SQL: Control Structures – Nested Blocks – SQL in
PL/SQL – Data Manipulation – Transaction Control statements. PL/SQL Cursors and Exceptions:
Cursors – Implicit Cursors, Explicit Cursors and Attributes – Cursor FOR loops – SELECT…FOR
UPDATE – WHERE CURRENT OF clause – Cursor with Parameters – Cursor Variables –
Exceptions – Types of Exceptions.
DATABASE MANAGEMENT SYSTEM
UNIT-I
Database Concepts:Database Systems - Data vs Information - Introducing the database -File system -
Problems with file system – Database systems. Data models - Importance - Basic Building
Blocks - Business rules - Evolution of Data models - Degrees of Data Abstraction.
DBMS:
A Database Management System (DBMS) is a software that enables users to define, create,
maintain, and control access to the database.
It provides an interface for performing various operations such as database creation, data storage,
updating, and deletion.
User
OSSoftware
Hardware
Flat File Systems: Initially, data was stored in flat files, which lacked structure and were prone to
redundancy and inconsistency.
Hierarchical and Network Models: Early DBMS, such as the IMS (Information Management
System) developed by IBM, implemented hierarchical data models. The network model
followed, allowing more complex relationships.
E.F. Codd's Relational Model: In 1970, Edgar F. Codd introduced the relational model, which
organized data into tables (relations). This revolutionized data management and led to the
development of SQL (Structured Query Language).
First Relational DBMS: Systems like IBM's System R and Oracle emerged, implementing Codd's
principles.
Growth and Standardization (1980s)
Commercialization: The 1980s saw the rise of commercial relational DBMS like Oracle, Ingres,
and Sybase.
SQL Standardization: The American National Standards Institute (ANSI) standardized SQL, which
became the standard language for interacting with relational databases.
Big Data Technologies: The explosion of data led to the development of technologies like
Hadoop and distributed databases to handle large volumes of data.
Cloud-Based DBMS: Cloud computing introduced new DBMS models, offering scalable and
managed database services (e.g., Amazon RDS, Google Cloud SQL).
Modern Trends
Components of a Database:
Types of Databases:
Relational Database: Uses tables with rows and columns (e.g., SQL-based).
NoSQL Database: For unstructured or semi-structured data (e.g., MongoDB, Redis).
Distributed Database: Data is spread across multiple physical locations.
Cloud Database: Hosted on cloud platforms (e.g., Amazon RDS, Google Firebase).
DATABASE SYSTEM:
1.Database:
A structured collection of data that can be easily accessed, managed, and updated.
Examples: MySQL, PostgreSQL, Oracle, MongoDB
2. Database Management System (DBMS)
Software that interacts with the user, applications, and the database itself to capture and analyze
data.
Functions include:
3. Data Models
Key operations:
6. Normalization
8. Indexing
9. Concurrency Control
DATA VS INFORMATION:
What is Data?
Data refers to raw, unprocessed facts and figures that lack context or interpretation on
their own. This information, in its raw form, can be difficult to understand or apply
without extra processing.
Text, multimedia (pictures, videos, audio), and numerical numbers are all acceptable
formats for data collection. These types of data are critical in many disciplines, including
science, business, and technology, where they serve as the foundation for analysis and
decision-making.
On their own, these numbers do not convey much meaning. However, when processed,
organized, and analyzed, this data can provide valuable insights.
What is Information?
Information is data that has been processed, organized, or structured to convey meaning and
significance.
Unlike raw data, information is more comprehensible and provides context that aids in
understanding the data.
The transformation from data to information generally involves several key steps:
1. Data Collection: Gathering raw data from various sources, such as weather stations, satellites,
or sensors.
2. Data Cleaning: Ensuring the data is accurate, consistent, and free from errors or outliers.
3. Data Analysis: Applying statistical methods and computational algorithms to identify patterns,
correlations, and trends within the data.
4. Data Interpretation: Making sense of the analyzed data by providing context and explaining
what the data signifies.
5. Data Presentation: Organizing the data in a coherent and visually appealing manner, such as
charts, graphs, or reports, to effectively communicate the information.
To illustrate, consider a dataset that includes the average temperature, humidity, and wind
direction of a city. When this data is processed and presented in an organized manner, it becomes
information that describes the city’s climate conditions
Data Information
It is a secondary level of
This is the primary level of intelligence.
intelligence.
Databases and database technology have a major impact on the growing use of
computers. It is fair to say that databases play a critical role in almost all areas
where computers are used, including business, electronic commerce, engineering,
medicine, genetics, law, education, and library science.
FILE SYSTEM:
The file system in a Database Management System (DBMS) refers to the way data is stored,
organized, and accessed on a storage medium. Here's an overview of how file systems operate
within a DBMS:
Key Concepts of File Systems in DBMS
1. Data Storage:
o Data is stored in files on a physical storage medium (like hard drives or SSDs).
o Files can be structured (e.g., tables in a relational database) or unstructured (e.g.,
images, documents).
2. File Organization:
o Heap Files: Unordered file organization where records are stored as they arrive.
o Sequential Files: Records are stored in a specific sequence based on a key attribute.
o Indexed Files: Files that use indexes to enhance data retrieval speed.
3. Access Methods:
o Sequential Access: Reading data in a linear fashion.
o Random Access: Accessing data directly through an index.
4. Buffer Management:
o Buffers are used to temporarily store data in memory to optimize reading and writing
operations.
o The DBMS manages buffer pools to reduce disk I/O operations and improve
performance.
5. File Formats:
o Data files can be in various formats, such as CSV, binary, or proprietary formats defined
by the DBMS.
6. Security and Integrity:
o The DBMS implements security measures (like access controls) to protect data stored in
files.
o Integrity constraints ensure that the data is accurate and consistent.
7. Backup and Recovery:
o The file system is responsible for managing backups and recovery processes to prevent
data loss.
The file system is a critical component of a DBMS, enabling efficient data storage, retrieval, and
management while ensuring data integrity and security
In DBMS data
independence exists, mainly of
two types:
Only one user can access Multiple users can access data
User Access data at a time. at a time.
File systems in Database Management Systems (DBMS) can encounter various problems that
may affect performance, reliability, and data integrity. Here are some common issues associated
with file systems in DBMS:
1. Data Corruption:
o Data can become corrupted due to unexpected shutdowns, hardware failures, or
software bugs.
o Corrupted files can lead to loss of data integrity and make it difficult to retrieve or
manipulate data.
2. Performance Issues:
o Fragmentation: Over time, files may become fragmented, leading to slower access
times as the system struggles to read scattered pieces of data.
o Inefficient Indexing: Poorly designed indexing structures can lead to slow query
performance and increased I/O operations.
3. Concurrency Control:
o When multiple users access and modify data simultaneously, it can lead to conflicts and
inconsistencies.
o Without proper locking mechanisms, data may be overwritten or left in an inconsistent
state.
4. Limited Scalability:
o Traditional file systems may struggle to handle large volumes of data or high transaction
rates, leading to performance bottlenecks.
o Upgrading or scaling the file system can be complex and costly.
5. Backup and Recovery Challenges:
o Inadequate backup strategies may result in data loss in case of failure.
o Recovery processes can be complicated and time-consuming if the file system does not
support efficient snapshots or rollbacks.
6. Security Vulnerabilities:
o File systems may be susceptible to unauthorized access, leading to data breaches.
o Inadequate encryption and access control measures can expose sensitive data.
7. Compatibility Issues:
o Different file formats or structures may create compatibility issues when integrating
with other systems or migrating to new platforms.
o Legacy systems may not support modern file formats, complicating data access and
management.
8. Resource Management:
o Inefficient use of storage resources can lead to wasted space, particularly if the file
system does not support dynamic resizing or compression.
o Poorly managed buffer pools can lead to increased disk I/O and reduced performance.
DATA MODEL:
Entity-Relationship (ER) Model is based on the notion of real-world entities and relationships among them.
While formulating real-world scenario into the database model, the ER Model creates entity set, relationship
set, general attributes and constraints.
RELATIONAL MODEL
The most popular data model in DBMS is the Relational Model. It is more scientific a
model than others. This model is based on first-order predicate logic and defines a
table as an n-ary relation
The main highlights of this model
Anentity is a person, place, thing, or event about which data will be collected and stored.
An entity represents a particular type of object in the real world, which means that an
entity is “distinguishable” that is, each occurrence is unique and distinct.
An entity may be a tangible object, i.e., one that you can touch such as a person or a
product. An entity may also be intangible, such as a flight route, or a rock concert (an
event).
An attribute is a characteristic of an entity. For example, a customer entity would be
described by attributes such as last name, first name, phone numbers, address, etc.. As we
will see, it is also possible for relationships to have attributes.
A relationship describes a bi-directional association among entities. For example, a
relationship exists between customers and sales agents that could be described as follows:
A sales agent can serve many customers, and each customer may be served by one sales
agent.
Data models use three types of relationships: one-to-many, many-to-many, and one-to-
one. Database designers and most data modeling tools use the shorthand notations 1:M or
1..* M:N or *..*, and 1:1 or 1..1, respectively.
, 1:M– a painter creates many different paintings, but each is painted by only one painter.
M:N– an employee may learn many job skills, and each job skill may be learned by many
employees.
1:1– Each department must have only one employee who is a manager and each
employee who is a m anager can manage only one dep
The fourth and final basic building block is a constraint, which is a restriction placed on the
data.
Business Rules:
A business rule is a brief, precise, and unambiguous description of a policy,
procedure, or principle within a specific organization.
Properly written business rules are used to define entities, attributes, relationship,
and constraints.
Any time you see relationship statements such as “an agent can server many
customers, and each customer can be served by only one agent,” business rules are
at work.
To be effective, business rules must be easy to understand and widely disseminated
to ensure that every person in the organization shares a common interpretation of
the rules. Business rules describe, in simple language, the main and distinguishing
characteristics of the data as viewed by the company
• Note that the business rules establish entities, relationships, and constraints. For example, the first two
business rules above establish two entities (Customer and Invoice) and a 1:M relationship between them.
The third business rule above establishes a constraint (no fewer than 10 people and no more than 30
people, two entities (Employee and Training), and a relationship between Employee and Training
The process of identifying and documenting business rules is essential to database design for
several reasons:
Theyhelp to standardize the organization’s view of data.
Theycan be a communication tool between users and designers
.– They allow the designer to understand the nature, role, and scope of the data
.– Theyallow the designer to understand business processes
.– They allow the designer to develop appropriate relationship participation rules and constraints
and to create an accurate data model.
• Not all business rules can be modeled. For example, a business rule that specifies “no pilot can fly
more than 10 hours within any 24 hour period” cannot be modeled. However, such a business rule can
be enforced by application software.
Business rules set the stage for the proper identification of entities, attributes, relationships,
and constraints. In the real world, names are used to identify object. If the business environment
wants to keep track of the objects, there will be specific business rules for the objects.
As a general rule, a noun in a business rule will translate to an entity in the model, and a verb
(active or passive) that associates the nouns will translate into a relationship among the entities.–
For example, the business rule “a customer may generate many invoices” contains two nouns
(customer and invoices) and a verb (generate) that associates the nouns. From this business rule,
you could deduce that
Customer and invoice are objects of interest for the environment and should be
represented by their respective entities.
• There is a “generate” relationship between customer and invoice.
Business Rules: Naming ConventionS
It is also good practice to prefix the name of an attribute with the name (or an
abbreviation) of the entity in which it occurs. called– For example, in the CUSTOMER
entity, the customer’s credit limit might be CUSTOMER_CREDIT_LIMIT,
CUS_CREDIT_LIMIT
The reason for this will become more apparent later on when you or perhaps learn about the
need to use common attributes to specify relationships between entities.
The use of a proper naming convention will improve the data model’s ability to facilitate
communication among the designer, application programmer, and the end users.
A proper naming convention can go a long way toward making your model self-documenting.
Evolution Of Data Model
Hierarchical Model
The hierarchical model was developed in the 1960s to manage large amounts of data from
complex manufacturing projects, such as the Apollo rocket program (moon landing in 1969).
The model’s basic logical structure is represented as a tree. The tree contains levels, or segments.
A segment is the equivalent of a file system’s record type. Within the hierarchy, a higher layer is
perceived as the parent of the segment directly beneath it, which is called the child.
The hierarchical model depicts a set of 1:M relationships between a parent and its children
segments. Each parent can have many children, but each child has only one parent
Network Model
The network model was created to represent complex data relationships more effectively than the
hierarchical model, to improve database performance, and to impose a database standard.
In the network model, the user perceives the database as a collection of records in 1:M
relationships. Unlike, the hierarchical model, the network model allows a record to have more than
one parent.
Although it is generally not used today, the network model defined many standards and concepts
that are still in use today, such as the terms schema and sub-schema and definitions for a data
manipulation language (DML) and data definition language (DDL)
RELATIONAL MODEL
The relational model was introduced in 1970 by E.F. Codd (working for IBM at the time), in his landmark
paper “A Relational Model of Data for Large Shared Databanks” (Communications of the ACM, June
1970, pp.377-387).
The relational model’s foundation is a mathematical concept known as a relation. To avoid the
complexity of abstract mathematical theory, think of a relation as a matrix composed of intersecting
rows and columns. Each row is called a tuple. Each column represents an attribute.
The relational model also describes a precise set of data manipulation constructs based on advanced
mathematical concepts.
The relational model is implemented through a very sophisticated relational database management
system (RDBMS). The RDBMS performs the same basic functions provided by the hierarchical and
network DBMS systems, in addition to a host of other functions that make the relational data model
easier to understand and implement.
Arguably, the most important advantage of the RDBMS is its ability to hide the complexities of the
relational model from the user.
The RDBMS manages all of the physical details, while the user sees the relational database as a
collection of tables in which the data are stored. The user can manipulate and query the data in a
way that seems intuitive and logical.
The end-user interface. Basically, the interface allows the end user to interact with the data (by
automatically generating SQL code). Each interface is a product of the software vendor’s ides of
meaningful interaction with the data
.– A collection of tables stored in the database. In a relational database, all data are perceived to be stored
in tables. The tables simply present the data to the end user in a way that is easy to understand. Each table is
independent. Rows in different tables are related by common values in common attributes.
– SQL engine. Largely hidden from the end user, the SQL engine executes all queries or data requests.
ncreasingly complex real-world problems demonstrated a need for a data model that more closely
represented the real world.
• In the object-oriented data model (OODM), both data and their relationships are contained in a single
structure known as an object. In turn, the OODM is the basis for the object-oriented database
management system (OODBMS).
• AnOODMreflects a very different way to define and use entities. Like the relational model’s entity, an
object is described by its factual content. But, quite unlike an entity, an object includes information
about relationships between the facts within the object, as well as information about its relationship
with other objects. Therefore, facts within the object are given greater meaning. The OODM is a
semantic data model.
Database systems include complex data-structures. In terms of retrieval of data, reduce complexity in terms
of usability of users and in order to make the system efficient, developers use levels of abstraction that hide
irrelevant details from the users. Levels of abstraction simplify database design.
Mainly there are three levels of abstraction for DBMS, which are as follows –
ONE MARK:
2. What is a database?
a) Organized collection of information that cannot be accessed, updated, and managed
b) Collection of data or information without organizing
c) Organized collection of data or information that can be accessed, updated, and managed
d) Organized collection of data that cannot be updated
View Answer
Answer: c
3. What is DBMS?
a) DBMS is a collection of queries
b) DBMS is a high-level language
c) DBMS is a programming language
d) DBMS stores, modifies and retrieves data
View Answer
Answer: d
Answer: d
6. In which of the following formats data is stored in the database management system?
a) Image
b) Text
c) Table
d) Grap
Answer: c
17. The values appearing in given attributes of any tuple in the referencing relation must likewise occur in
specified attributes of at least one tuple in the referenced relation, according to _____________________
integrity constraint.
a) Referential
b) Primary
c) Referencing
d) Specific
View Answer
Answer: a.
18. _____________ is a hardware component that is most important for the operation of a database
management system.
a) Microphone
b) High speed, large capacity disk to store data
c) High-resolution video display
d) Printer
View Answer
Answer: b.
19. The DBMS acts as an interface between ________________ and ________________ of an enterprise-
class system.
a) Data and the DBMS
b) Application and SQL
c) Database application and the database
d) The user and the software
View Answer
Answer: c
20. The ability to query data, as well as insert, delete, and alter tuples, is offered by ____________
a) TCL (Transaction Control Language)
b) DCL (Data Control Language)
c) DDL (Data Definition Langauge)
d) DML (Data Manipulation Langauge)
View Answer
Answer: d
21. ______________ is a set of one or more attributes taken collectively to uniquely identify a record.
a) Primary Key
b) Foreign key
c) Super key
d) Candidate key
View Answer
Answer: c
Explanation: Foreign key creates a relationship between two relations. Super key is the superset of all the
keys in a relation. A candidate key is used to identify tuples in a relation.
23. Which of the following set should be associated with weak entity set for weak entity to be
meaningful?
a) Neighbour set
b) Strong entity set
c) Owner set
d) Identifying set
View Answer
Answer: d
24. Which of the following command is correct to delete the values in the relation teaches?
a) Delete from teaches;
b) Delete from teaches where Id =’Null’;
c) Remove table teaches;
d) Drop table teaches;
View Answer
Answer: a
28. The top level of the hierarchy consists of ______ each of which can contain _____.
a) Schemas, Catalogs
b) Schemas, Environment
c) Environment, Schemas
d) Catalogs, Schemas
View Answer
Answer: d
29. _______ indicates the maximum number of entities that can be involved in a relationship.
a) Greater Entity Count
b) Minimum cardinality
c) Maximum cardinality
d) ERD
View Answer
Answer: c
30. The user IDs can be added or removed using which of the following fixed roles?
a) db_sysadmin
b) db_accessadmin
c) db_securityadmin
d) db_setupadmin
View Answer
Answer: b
32. The traditional storage of data organized by the customer, stored in separate folders in filing cabinets
is an example of ______________ type of ‘database’ management system.
a) Object-oriented database management system
b) Relational database management system
c) Network database management system
d) Hierarchical database management system
View Answer
Answer: d
33. After groups have been established, SQL applies predicates in the ___________ clause, allowing
aggregate functions to be used.
a) Where
b) Having
c) Group by
d) With
View Answer
Answer: b
35. What does a foreign key combined with a primary key create?
a) Network model between the tables that connect them
b) Parent-Child relationship between the tables that connects them
c) One to many relationship between the tables that connects them
d) All of the mentioned
View Answer
Answer: b
36. Which of the following is correct according to the technology deployed by DBMS?
a) Pointers are used to maintain transactional integrity and consistency
b) Cursors are used to maintain transactional integrity and consistency
c) Locks are used to maintain transactional integrity and consistency
d) Triggers are used to maintain transactional integrity and consistency
View Answer
Answer: c
Explanation: Pointers are used to access data with great speed and accuracy. Consistency is maintained
using locks.
37. Which of the following is correct regarding the file produced by a spreadsheet?
a) can be used as it is by the DBMS
b) stored on disk in an ASCII text format
c) all of the mentioned
d) none of the mentioned
View Answer
Answer: a
40. The query specifying the SQL view is said to be updatable if it meets which of the following
conditions?
a) select clause contains relation attribute names but not have expressions, aggregates, or distinct
specification
b) from clause has 1 relation
c) query does not have group by or having clause
d) All of the mentioned
View Answer
Answer: d
41. When the “ROLLUP” operator for expression or columns within a “GROUP BY” clause is used?
a) Find the groups that make up the subtotal in a row
b) Create group-wise grand totals for the groups indicated in a GROUP BY clause
c) Group expressions or columns specified in a GROUP BY clause in one direction, from right to left, for
computing the subtotals
d) To produce a cross-tabular report for computing subtotals by grouping phrases or columns given within
a GROUP BY clause in all available directions
View Answer
Answer: c
42. Which of the following is the best way to represent the attributes in a large db?
a) Dot representation
b) Concatenation
c) Relational-and
d) All of the mentioned
View Answer
Answer: b
43. Which of the following is the subset of SQL commands used to manipulate Oracle Structures,
including tables?
a) Data Described Language
b) Data Retrieval Language
c) Data Manipulation Language
d) Data Definition Language
View Answer
Answer: d.
44. Which of the following functions construct histograms and use buckets for ranking?
a) Ntil()
b) Newtil()
c) Rank()
d) All of the mentioned
View Answer
Answer: a
45. __________ command is used in SQL to issue multiple CREATE TABLE, CREATE VIEW and
GRANT statements in a single transaction.
a) CREATE CLUSTER
b) CREATE PACKAGE
c) CREATE SCHEMA
d) All of the mentioned
View Answer
46. Which of the following key is required in to handle the data when the encryption is applied to the data
so that the unauthorised user cannot access the data?
a) Primary key
b) Authorised key
c) Encryption key
d) Decryption key
View Answer
Answer: c
47. Which of the following is known as the process of viewing cross-tab with a fixed value of one
attribute?
a) Dicing
b) Pivoting
c) Slicing
d) Both Pivoting and Dicing
View Answer
Answer: c
48. For designing a normal RDBMS which of the following normal form is considered adequate?
a) 4NF
b) 3NF
c) 2NF
d) 5NF
View Answer
Answer: b
49. Which of the following is popular for applications such as storage of log files in a database
management system since it offers the best write performance?
a) RAID level 0
b) RAID level 1
c) RAID level 2
d) RAID level 3
View Answer
Answer: b
50. Which of the following represents a query in the tuple relational calculus?
a) { }{P(t) | t }
b) {t | P(t)}
c) t | P() | t
d) All of the mentioned
View Answer
Answer: b
5MARK:
10 MARK:
UNIT I COMPLETED
UNIT II
Design Concepts: Relational database model - logical view of data-keys -Integrity rules -
relational set operators - data dictionary and the system catalog - relationships -data
redundancy revisited -indexes - codd's rules. Entity relationship model - ER diagram
DESIGN CONCEPT:
The main objectives of database design in DBMS are to produce logical and physical designs
models of the proposed database system.
The logical model concentrates on the data requirements and the data to be stored independent of
physical considerations. It does not concern itself with how the data will be stored or where it will
be stored physically.
The physical data design model involves translating the logical DB design of the database onto
physical media using hardware resources and software systems such as database management
systems (DBMS).
Database design process in DBMS is crucial for high performance database system. the genius of a
database is in its design. Data operations using SQL is relatively simple
The database development life cycle has a number of stages that are followed when developing
database systems.
The steps in the development life cycle do not necessarily have to be followed religiously in a
sequential manner.
On small database systems, the process of database design is usually very simple and does not
involve a lot of steps.
In order to fully appreciate the above diagram, let’s look at the individual components listed in
each step for overview of design process in DBMS.
Requirements analysis
Planning – This stages of database design concepts are concerned with planning of entire
Database Development Life Cycle. It takes into consideration the Information Systems strategy
of the organization.
System definition – This stage defines the scope and boundaries of the proposed database
system.
Database designing
Logical model – This stage is concerned with developing a database model based on
requirements. The entire design is on paper without any physical implementations or specific
DBMS considerations.
Physical model – This stage implements the logical model of the database taking into account
the DBMS and physical implementation factors.
Implementation
Data conversion and loading – this stage of relational databases design is concerned with
importing and converting data from the old system into the new database.
Testing – this stage is concerned with the identification of errors in the newly implemented
system. It checks the database against requirement specifications.
Relational Model?
The table name and column names are helpful to interpret the meaning of values in each
row. The data are represented as a set of relations. In the relational model, data are stored
as tables. However, the physical storage of the data is independent of the way the data are
logically organized.
Relational Model Concepts in DBMS
1. Attribute: Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is stored along
with its entities. A table has two properties rows and columns. Rows represent records and
columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system. Relation
instances never have duplicate tuples.
9. Relation key – Every row has one, two or multiple attributes, which is called relation key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is known as
attribute domain
There are many types of Integrity Constraints in DBMS. Constraints on the Relational database
management system is mostly divided into three main categories are:
1. Domain Constraints
2. Key Constraints
3. Referential Integrity Constraints
Domain Constraints
Domain constraints can be violated if an attribute value is not appearing in the
corresponding domain or it is not of the appropriate data type.
Domain constraints specify that within each tuple, and the value of each attribute must be
unique. This is specified as data types which include standard data types integers, real
numbers, characters, Booleans, variable length strings, etc.
Example:
The example shown demonstrates creating a domain constraint such that CustomerName is not
NULL
To fully understand the view of data, you must have a basic knowledge of data abstraction and
instance & schema. Refer these two tutorials to learn them in detail.
1. Data abstraction:Database systems are made-up of complex data structures. To ease the user
interaction with database, the developers hide internal irrelevant details from users. This process of
hiding irrelevant details from user is called data abstraction.
2. Instance and schema: Design of a database is called the schema. Schema is of three types: Physical
schema, logical schema and view schema. The data stored in database at a particular moment of
time is called instance of database. Database schema defines the variable declarations in tables that
belong to a particular database; the value of these variables at a moment of time is called the
instance of that database.
For example: When you are booking a train ticket, you are not concerned how data is
processing at the back end when you click “book ticket”, what processes are happening when
you are doing online payments. You are just concerned about the message that pops up when
your ticket is successfully booked. This doesn’t mean that the process happening at the back
end is not relevant, it just means that you as a user are not concerned what is happening in the
database.
Physical level: This is the lowest level of data abstraction. It describes how data is actually
stored in database. You can get the complex data structure details at this level.
Logical level: This is the middle level of 3-level data abstraction architecture. It describes what
data is stored in database.
View level: Highest level of data abstraction. This level describes the user interaction with
database system.
Example: Let’s say we are storing customer information in a customer table. At physical
level these records can be described as blocks of storage (bytes, gigabytes, terabytes etc.) in
memory. These details are often hidden from the programmers.
At the logical level these records can be described as fields and attributes along with their data
types, their relationship among each other can be logically implemented. The programmers
generally work at this level because they are aware of such things about database systems.
At view level, user just interact with system with the help of GUI and enter the details at the
screen, they are not aware of how the data is stored and what data is stored; such details are
hidden from them.
DBMS Schema
Definition of schema: Design of a database is called the schema. For example:
An employee table in database exists with the following attributes:
Schema represents the logical view of the database. It helps you understand what data needs to go
where.
Schema can be represented by a diagram as shown below.
Schema helps the database users to understand the relationship between data. This helps in
efficiently performing operations on database such as insert, update, delete, search etc.
In the following diagram, we have a schema that shows the relationship between three tables:
Course, Student and Section. The diagram only shows the design of the database, it doesn’t
show the data present in those tables. Schema is only a structural view(design) of a database as
shown in the diagram below.
The design of a database at physical level is called physical schema, how the data stored in
blocks of storage is described at this level.
Design of database at logical level is called logical schema, programmers and database
administrators work at this level, at this level data can be described as certain types of data
records gets stored in data structures, however the internal details such as implementation of data
structure is hidden at this level (available at physical level).
Design of database at view level is called view schema. This generally describes end user
interaction with database systems.
To learn more about these schemas, refer 3 level data abstraction architecture.
DBMS Instance
Definition of instance: The data stored in database at a particular moment of time is called
instance of database. Database schema defines the attributes in tables that belong to a particular
database. The value of these attributes at a moment of time is called the instance of that database.
For example, we have seen the schema of table “employee” above. Let’s see the table with the
data now. At this moment the table contains two rows (records). This is the the current instance
of the table “employee” because this is the data that is stored in this table at this particular
moment of time.
In DBMS systems, integrity rules, which take a prime place, are designed to ensure that the
quality of data is always high, with no inconsistencies or errors. The set of principles, also
known as the integrity rules or constraints, helps to manage the data stored in the system in the
right way and determine whether it’s suitable for certain purposes. It is not without reason that
these rules are so crucial since they are key to the overall integrity of the database and its
quality. Herein, we investigate the centrality of governance regulations, illustrate the different
types that exist, and provide their implementation in DBMS.
What are Integrity Rules?
Integrity rules normally represent the pre-existing conditions, or constraints, of data stored in
the database that will ensure the data is valid and consistent. These rules, defining permissible
values, relationships, and data operations enclosed in the database system, ensure the accuracy
and reliability of the data to be used for its purposes. Rules for integrity are considered barriers
towards the possibility of some operations over the database, such as deletion, modification, or
insertion of data whose accuracy is doubtful or inconsistent, thus enabling the database to be of
high quality.
Types of Integrity Rules
Entity Integrity: Also called business key integrity, this rule proclaims that each row in a
table must contain some unique data, which is known as the business key. Through this
verification, a primary key will be ensured of non-duplication, which also implies that no
null values are allowed in the primary key column, thus uniquely identifying each instance
of an entity in the table.
Referential Integrity: Referential integrity guarantees the consistent pass of key reference
constraints between data through main table and foreign keys. This rule means that for
every foreign key value in a child table, that value has to possess the same primary key
value in the parent table. This is the only way to maintain foreign keys and make father
child relationships work properly.
Domain Integrity: Verifying domain validity means finding out if the value lies within the
allowed range for a particular attribute's column in the data table. It implements data
integrity by specifying data formats, ranges, and constraints, for example,
check constraints and limitations, that ensure that all officially recognized data are kept.
User-defined Integrity: This is an enterprise-specific set of integrity rules that would be
established by different organizations depending on their business edges and the rules
ascribed to sensitive data. These rules can be the user-defined ones like business rules or
validation criteria or can even be the custom constraints that are different from organization
to organization.
Implementation of Integrity Rules
Integrity rules are typically implemented through a combination of mechanisms within
the DBMS, including:
Constraints: Constraints including a primary key constraint, a foreign key constraint, a
unique constraint, and a check constraint are to maintain the integrity rules at the schema
level.
Triggers: Triggers are database elements that are activated on the fly to conduct given
activities (executed on insert, update, or delete operations, for example) on specific
database tables. Invoking triggers provides leverage to impose tougher restrictions, thus
enabling the enforcement of more advanced integrity rules or conducting validation
operations, which are a more complex kind of comparison beyond checks of constraints.
Stored Procedures: Stored procedures are precompiled sets of SQL operations that specify
business logic and data processing operations, and they are stored in a database. They may
need to be applied to the forces of rule-following and data consistency tasks during the
events of transactions.
Significance of Integrity Rules
The rules of integrity, including the truth, credibility, and consistency, is one of the key
elements in ensuring proper functioning of the data in database. Their significance extends to
various aspects of database management, including:Their significance extends to various
aspects of database management, including:
Data Quality: Integrity rules make sure data is precisely moderated, elaborated, and of
high- quality in the database, just like the data quality and reliability.
Data Security: Integrity rules classify together while limiting and validating requests. They
are effective in defending against unauthorized access, data breaches, and data
manipulation attacks.
Data Consistency: Ethical parameters help create a homogeneous database, preventing
data inconsistencies, anomalies, and errors that could arise from invalid and incorrect data
keystrokes, hence allowing for data stemming from valid data sources that are consistently
coherent and reliable both qualitatively and quantitatively.
Compliance: Extent rules make sure all regulations, industry standards, and company
policies governing data security, privacy, and management are complied with.
In Conclusion, integrity regulations constitute the fundamental codes of the DBMS that assure
proper, correct, and readable data. Through the specification and enforcement of the normal
definition as well as relationships and actions, the integrity rules preserve the data integrity and
the trust in the database systems. The correct place of integrity rules in the data industry is to
take further steps in ensuring that data is of good quality, secure, and in compliance with
regulations. With the increasing demand to employ data as an imperative strategic resource,
sticking to the rules that are related to information safety becomes a must for ensuring the
trustworthiness and dependability of database schemes.
Relational Algebra in DBMS These Set Theory operations are the standard mathematical
operations on set. These operations are Binary operations that are, operated on 2 relations
unlike PROJECT, SELECT and RENAME operations. These operations are used to merge 2
sets in various ways.
The set operation is mainly categorized into the following:
1. Union operation
2. Intersection operation
3. Set difference or Minus operation
Before we apply one of the 3 set operations on relations, the two relations on which we are
performing the operations must have same type of tuples. This is also known as be Union
compatibility (or Type compatibility).
Type compatibility: Two relations A(P1, P2, ..., Pn) and B(Q1, Q2, ..., Qn) are said to be
Type compatible (or Union compatible) if both the relation have the same degree 'k' and
domain(Pi) = domain(Qi) for 1<= i <= k.
1. UNION Operation: Notation:
A∪S
where, A and S are the relations, symbol ‘∪’ is used to denote the Union operator. The result
of Union operation, which is denoted by A ∪ S, is a relation that basically includes all the
tuples that are present in A or in S, or in both, eliminating the duplicate tuples.
Set Operators
⇒ Symbol: ∩
⇒ Syntax: R ∩ S
⇒ Relational Algebra: R ∩ S
This version ensures the conditions for compatibility are clear and corrects any grammatical or conceptual
errors.
Difference ( - ) Operator
Suppose R and S are two relations. The Set Difference operation selects all the tuples that are present in the
first relation R but not in the second relation S.
⇒ Symbol: -
⇒ Syntax: R - S
⇒ Relational Algebra: R – S
It stores all information about relationships or tables, from the schema and constraints used.
All metadata is preserved. In general, metadata refers to information about data. Thus,
storing the connection scheme and other metadata in a single structure called a data
dictionary or system directory. A data dictionary is like an A-Z dictionary of a relational
database system that stores all the information about every relationship in the database.
What is a Data Dictionary?
The data dictionary consists of two words, data, which represents data collected from several
sources, and dictionary, which represents where this data is available. The data dictionary
is an important part of the relational database because it provides additional
information about the relationship between several tables in the database. A data dictionary
in a DBMS helps users manage data in an orderly and orderly manner, thereby
preventing data redundancy.
Below is a data dictionary that describes the table that contains employee details.
Field Size for
Field Name Data Type Description Example
Display
Unique ID of each
EmployeeID Integer 8 100025
employee
Emily
FullName Text 30 Full name of the employee
Johnson
There are basically two types of data dictionaries in a database management system:
Integrated Data Dictionary
Stand Alone Data Dictionary
Integrated Data Dictionary
Every relational database has an Integrated Data Dictionary available in the DBMS. This
integrated data dictionary acts as a system directory that is accessed and updated by the
relational database. The old database does not have an integrated data dictionary, so the
database administrator must use the Stand Alone Data Dictionary. An Integrated Data
Dictionary in a DBMS can link metadata.
The integrated data dictionary can be further divided into two types:
Active: When any changes are made to the database, the active data dictionary is
automatically updated by the DBMS. It is also known as a self-updating dictionary because it
continuously updates its data.
Passive: Unlike active dictionaries, passive dictionaries must be updated manually when
there are changes in the database. This type of data dictionary is difficult to manage because
it requires proper functionality. Else, the database and data dictionary will be synchronized.
Stand Alone Data Dictionary
This type of database in the DBMS is very adaptive because it grants the administrator in
charge of the confidential information complete autonomy to define and manage all crucial
data. Whether the information is printed or not has nothing to do with it. A data dictionary
that has a stand-alone format enables database designers to have the flexibility to
communicate with end users regardless of their data dictionaries format.
There is no standard format for data dictionaries. Here are some common elements:
Data Elements: The data dictionary describes each data element by specifying the names,
data types, storage formats and validation rules.
Table: All information about the table, such as the user who created the table, the number
of rows and columns, the date the table was created and entered, etc.
Indexes: Indexes for database tables are stored in the data dictionary. The DBMS stores
the index name used and index attributes, locations, and properties, as well as the creation
date, in each index.
Programs: Applications defined for database access, reports, application formats and
screens, SQL queries, etc. also stored in the data dictionary.
Relationships between data elements: A data dictionary stores relationship types; for
example, if it is mandatory or optional, the nature of the relationship and connection, etc.
Administration and End Users: The data dictionary stores all administrative and end user
data.
Metadata in a DBMS, stored in a data dictionary, is like a monitor that controls database
usage and whether users are allowed to access the database.
How to Create a Data Dictionary?
As mentioned above, most businesses rely on a database management system that has an
integrated data dictionary because it is automatically updated and easy to maintain.
Documentation for databases including MySQL, SQL Server, Oracle, etc. This can be done
in various relational databases such as
Database administrators can use templates in SQL Server, Oracle, or Microsoft Excel to
create a stand-alone data dictionary
Composition = is composed of
Sequence + AND
Selection [|] OR
Employee Table:
Column Name Data Type Description
The Data Dictionary in the DBMS provides additional information about the
relationship between several database tables, helps organize data, and prevents data
redundancy in the DBMS.
A database dictionary is a set of files that contain database metadata. Therefore, it is
also known as metadata repository.
The data dictionary in the database management system is wide and consists of two
types:
o Integrated Data Dictionary
o Stand Alone Data Dictionary
This article discusses the topic of Data Dictionary in DBMS in detail. We have seen
definitions, examples, types, needs, advantages of Data Dictionary in DBMS.
We hope this blog has helped you to improve your knowledge about Data Dictionary in
DBMS.
What is Data Catalog?
As enterprise data volumes explode into petabyte scale, getting a handle on burgeoning and
ever-changing data assets becomes imperative. But siloed, scattered data lakes provide little
strategic value. This is where a meticulous data catalogue comes in - to bring order to the
data deluge. When executed effectively, data cataloguing delivers a unified view of the
organization's information landscape that powers productivity and innovation.
In this article, We will explore Data Cataloging, its features, Benefits, Implementation, and
Role of Data Cataloging.
Redundancy means having multiple copies of the same data in the database. This problem arises
when a database is not normalized. Suppose a table of student details attributes is: student ID,
student name, college name, college rank, and course opted.
Name
Student_ID Contact College Course Rank
It can be observed that values of attribute college name, college rank, and course are being
repeated which can lead to problems. Problems caused due to redundancy are:
Insertion anomaly
Deletion anomaly
Updation anomaly
Insertion Anomaly
If a student detail has to be inserted whose course is not being decided yet then insertion will not
be possible till the time course is decided for the student.
Student_ID
Name Contact College Course Rank
This problem happens when the insertion of a data record is not possible without adding some
additional unrelated data to the record.
Deletion Anomaly
If the details of students in this table are deleted then the details of the college will also get
deleted which should not occur by common sense. This anomaly happens when the deletion of a
data record results in losing some unrelated information that was stored as part of the record that
was deleted from a table.
It is not possible to delete some information without losing some other information in the table as
well.
Updation Anomaly
Suppose the rank of the college changes then changes will have to be all over the database which
will be time-consuming and computationally costly.
Student_ID Name Contact College Course Rank
All places should be updated, If updation does not occur at all places then the database will be in
an inconsistent state.
Redundancy in a database occurs when the same data is stored in multiple places. Redundancy
can cause various problems such as data inconsistencies, higher storage requirements, and slower
data retrieval.
Problems Caused Due to Redundancy
Data Inconsistency: Redundancy can lead to data inconsistencies, where the same data is
stored in multiple locations, and changes to one copy of the data are not reflected in the other
copies. This can result in incorrect data being used in decision-making processes and can lead
to errors and inconsistencies in the data.
Storage Requirements: Redundancy increases the storage requirements of a database. If the
same data is stored in multiple places, more storage space is required to store the data. This
can lead to higher costs and slower data retrieval.
Update Anomalies: Redundancy can lead to update anomalies, where changes made to one
copy of the data are not reflected in the other copies. This can result in incorrect data being
used in decision-making processes and can lead to errors and inconsistencies in the data.
Performance Issues: Redundancy can also lead to performance issues, as the database must
spend more time updating multiple copies of the same data. This can lead to slower data
retrieval and slower overall performance of the database.
Security Issues: Redundancy can also create security issues, as multiple copies of the same
data can be accessed and manipulated by unauthorized users. This can lead to data breaches
and compromise the confidentiality, integrity, and availability of the data.
Maintenance Complexity: Redundancy can increase the complexity of database
maintenance, as multiple copies of the same data must be updated and synchronized. This can
make it more difficult to troubleshoot and resolve issues and can require more time and
resources to maintain the database.
Data Duplication: Redundancy can lead to data duplication, where the same data is stored in
multiple locations, resulting in wasted storage space and increased maintenance complexity.
This can also lead to confusion and errors, as different copies of the data may have different
values or be out of sync.
Data Integrity: Redundancy can also compromise data integrity, as changes made to one
copy of the data may not be reflected in the other copies. This can result in inconsistencies
and errors and can make it difficult to ensure that the data is accurate and up-to-date.
Usability Issues: Redundancy can also create usability issues, as users may have difficulty
accessing the correct version of the data or may be confused by inconsistencies and errors.
This can lead to frustration and decreased productivity, as users spend more time searching
for the correct data or correcting errors.
To prevent redundancy in a database, normalization techniques can be used. Normalization is the
process of organizing data in a database to eliminate redundancy and improve data
integrity. Normalization involves breaking down a larger table into smaller tables and
establishing relationships between them. This reduces redundancy and makes the database more
efficient and reliable.
Advantages of Redundant Data
Enhanced Query Performance: By eliminating the need for intricate joins, redundancy
helps expedite data retrieval.
Offline Access: In offline circumstances, redundant copies allow data access even in the
absence of continuous connectivity.
Increased Availability: Redundancy helps to increase fault tolerance, which makes data
accessible even in the event of server failures.
Disadvantages of Redundant Data
Increased storage requirements: Redundant data takes up additional storage space within
the database, which can increase costs and slow down performance.
Inconsistency: If the same data is stored in multiple places within the database, there is a risk
that updates or changes made to one copy of the data may not be reflected in other copies,
leading to inconsistency and potentially incorrect results.
Difficulty in maintenance: With redundant data, it becomes more difficult to maintain the
accuracy and consistency of the data. It requires more effort and resources to ensure that all
copies of the data are updated correctly.
Increased risk of errors: When data is redundant, there is a greater risk of errors in
the database. For example, if the same data is stored in multiple tables, there is a risk of
inconsistencies between the tables.
Reduced flexibility: Redundancy can reduce the flexibility of the database. For example, if a
change needs to be made to a particular piece of data, it may need to be updated in multiple
places, which can be time-consuming and error-prone.
In databases, data redundancy is a prevalent issue. It can cause a number of problems , such
as inconsistent data, wasted storage space, decreased database performance, and increased
security risk.
The most effective technique to reduce redundancy is to normalize the database. The use of
views materialized views, and foreign keys are additional techniques to reduce redundancy
introduction of ER Model
The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases.
This model represents the logical structure of a database, including entities, their attributes
and relationships between them.
Entity: An objects that is stored as data such as Student, Course or Company.
Attribute: Properties that describes an entity such as StudentID, CourseName,
or EmployeeEmail.
Relationship: A connection between entities such as "a Student enrolls in a Course".
Components of ER Diagram
What is an Entity?
An Entity represents a real-world object, concept or thing about which data is stored in a
database. It act as a building block of a database. Tables in relational database represent these
entities.
Example of entities:
Real-World Objects: Person, Car, Employee etc.
Concepts: Course, Event, Reservation etc.
Things: Product, Document, Device etc.
The entity type defines the structure of an entity, while individual instances of that type
represent specific entities.
What is an Entity Set?
An entity refers to an individual object of an entity type, and the collection of all entities of a
particular type is called an entity set. For example, E1 is an entity that belongs to the entity
type "Student," and the group of all students forms the entity set.
In the ER diagram below, the entity type is represented as:
Entity Set
We can represent the entity sets in an ER Diagram but we can't represent individual entities
because an entity is like a row in a table, and an ER diagram shows the structure and
relationships of data, not specific data entries (like rows and columns). An ER diagram is a
visual representation of the data model, not the actual data itself.
Types of Entity
There are two main types of entities:
1. Strong Entity
A Strong Entity is a type of entity that has a key Attribute that can uniquely identify each
instance of the entity. A Strong Entity does not depend on any other Entity in the Schema for
its identification. It has a primary key that ensures its uniqueness and is represented by a
rectangle in an ER diagram.
2. Weak Entity
A Weak Entity cannot be uniquely identified by its own attributes alone. It depends on a
strong entity to be identified. A weak entity is associated with an identifying entity (strong
entity), which helps in its identification. A weak entity are represented by a double rectangle.
The participation of weak entity types is always total. The relationship between the weak
entity type and its identifying strong entity type is called identifying relationship and it is
represented by a double diamond.
Example:
A company may store the information of dependents (Parents, Children, Spouse) of an
Employee. But the dependents can't exist without the employee. So dependent will be a
Weak Entity Type and Employee will be identifying entity type for dependent, which means
it is Strong Entity Type.
Attribute
Types of Attributes
1. Key Attribute
The attribute which uniquely identifies each entity in the entity set is called the key attribute.
For example, Roll_No will be unique for each student. In ER diagram, the key attribute is
represented by an oval with an underline.
Key Attribute
2. Composite Attribute
An attribute composed of many other attributes is called a composite attribute. For example,
the Address attribute of the student Entity type consists of Street, City, State, and Country. In
ER diagram, the composite attribute is represented by an oval comprising of ovals.
Composite Attribute
3. Multivalued Attribute
An attribute consisting of more than one value for a given entity. For example, Phone_No
(can be more than one for a given student). In ER diagram, a multivalued attribute is
represented by a double oval.
Multivalued Attribute
4. Derived Attribute
An attribute that can be derived from other attributes of the entity type is known as a derived
attribute. e.g.; Age (can be derived from DOB). In ER diagram, the derived attribute is
represented by a dashed oval.
Derived Attribute
The Complete Entity Type Student with its Attributes can be represented as:
Entity-Relationship Set
A set of relationships of the same type is known as a relationship set. The following
relationship set depicts S1 as enrolled in C2, S2 as enrolled in C1, and S3 as registered in C3.
Relationship Set
Unary Relationship
2. Binary Relationship: When there are TWO entities set participating in a relationship, the
relationship is called a binary relationship. For example, a Student is enrolled in a Course.
Binary Relationship
3. Ternary Relationship: When there are three entity sets participating in a relationship, the
relationship is called a ternary relationship.
4. N-ary Relationship: When there are n entities set participating in a relationship, the
relationship is called an n-ary relationship.
Cardinality in ER Model
The maximum number of times an entity of an entity set participates in a relationship set is
known as cardinality.
Cardinality can be of different types:
1. One-to-One
When each entity in each entity set can take part only once in the relationship, the cardinality
is one-to-one. Let us assume that a male can marry one female and a female can marry one
male. So the relationship will be one-to-one.
In this case, each student is taking only 1 course but 1 course has been taken by many
students.
4. Many-to-Many
When entities in all entity sets can take part more than once in the relationship cardinality is
many to many. Let us assume that a student can take more than one course and one course
can be taken by many students. So the relationship will be many to many.
Many-to-Many
Set Representation
In this example, student S1 is enrolled in C1 and C3 and Course C3 is enrolled by S1, S3,
and S4. So it is many-to-many relationships.
Participation Constraint
Participation Constraint is applied to the entity participating in the relationship set.
1. Total Participation: Each entity in the entity set must participate in the relationship. If each student
must enroll in a course, the participation of students will be total. Total participation is shown by a double
line in the ER diagram.
2. Partial Participation: The entity in the entity set may or may NOT participate in the relationship. If
some courses are not enrolled by any of the students, the participation in the course will be partial.
The diagram depicts the ‘Enrolled in’ relationship set with Student Entity set having total participation
and Course Entity set having partial participation.
Every student in the Student Entity set participates in a relationship but there exists a course C4 that is not
taking part in the relationship.
How to Draw an ER Diagram
1. Identify Entities: The very first step is to identify all the Entities. Represent these entities in a
Rectangle and label them accordingly.
2. Identify Relationships: The next step is to identify the relationship between them and represent them
accordingly using the Diamond shape. Ensure that relationships are not directly connected to each other.
3. Add Attributes: Attach attributes to the entities by using ovals. Each entity can have multiple
attributes (such as name, age, etc.), which are connected to the respective entity.
4. Define Primary Keys: Assign primary keys to each entity. These are unique identifiers that help
distinguish each instance of the entity. Represent them with underlined attributes.
5. Remove Redundancies: Review the diagram and eliminate unnecessary or repetitive entities and
relationships.
6. Review for Clarity: Review the diagram make sure it is clear and effectively conveys the relationships
between the entities.
ONE MARK
A. Table
B. Database
C. Row
D. Column
Answer: B) Database
A. Data
B. Fields
C. Redundancy
D. Database
A. Insert
B. Update
C. Delete
D. All of the above
A. Abnormal
B. Normal
C. Special
D. None
Answer: B) Normal
A. 3
B. 4
C. 5
D. 6
Answer: B) 4
A. 1NF
B. 2NF
C. 3NF
D. 10NF
Answer: D) 10NF
A. 1NF
B. 2NF
C. 3NF
D. BCNF
Answer: A) 1NF
10. 2NF relations are those that are in 1NF with all the attribute types dependent on the ___ key.
A. Primary
B. Foreign
C. Composite
D. Alternate
Answer: A) Primary
A. Transition Dependency
B. No Transition Dependency
C. Relational Dependency
D. No Relational Dependency
12. A relation is in ___ if it is in Boyce Codd normal form and does not have any multivalued
dependencies.
A. 1NF
B. 2NF
C. 3NF
D. 4NF
Answer: D) 4NF
13. If a relation has a 4NF and no join dependency, and when it joins, it should be___, it is
considered 5NF.
A. Lossful
B. Lesser
C. Lossless
D. Full
Answer: C) Lossless
15. Neither multivalued nor composite attributes, nor their combinations, may be used in the ___
normal form.
A. First
B. Second
C. Third
D. fourth
Answer: A) First
A. When a relation is in 2NF and does not contain transitive partial dependencies, it will be in 3NF.
B. Data duplication is reduced by using 3NF.
C. It helps maintain the integrity of the data.
D. All of the above
18. Non-prime attributes cannot be transitively dependent, so the relation must have the ___
normal form.
A. First
B. Second
C. Third
D. Fourth
Answer: C) Third
19. There needs to be which of the following conditions for each nontrivial dependency of function
X on function Y for a relation to be in third normal form.
A. A super key is X.
B. Every element of Y is a part of some candidate key, i.e, Y is a prime attribute.
C. Either A or B
D. None of the above
Answer: C) Either A or B
21. A relation is in ___ if it is in Boyce Codd normal form and does not have any multivalued
dependencies.
A. 1NF
B. 2NF
C. 3NF
D. 4NF
Answer: D) 4NF
22. If more than one value of B is present for a single value of A in a dependency A -> B, then the
relationship is ___.
A. Single
B. Multi-valued
C. Both a and b
D. None of the above
Answer: B) Multi-valued
A. A relation is in 5NF if it is in 4NF, does not contain any join dependencies, and has lossless
joining.
B. In order to avoid redundancy, 5NF ensures that the tables are broken up in as many ways as
possible.
C. Project-join normal form (5NF) is sometimes referred to as Project-join NF.
D. All of the above
a.Physical Level
b.Critical Level
c.Logical Level
d.View Level
25. What is the basic client/server architecture, one has to deal with?
A. 1
B. 2
C. 3
D. 4
Answer: C) 3
A. 2-tier architecture
B. 3-tier architecture
C. 4-tier architecture
D. 5-tier architecture
A. ODBC
B. JDBC
C. Both A. and B.
D. None of the above
31. In which architecture, another layer is there between client and server?
A. 1-tier architecture
B. 2-tier architecture
C. 3-tier architecture
D. 4-tier architecture
32. In which architecture, client can't directly communicate with the server?
A. 1-tier architecture
B. 2-tier architecture
C. 3-tier architecture
D. None of the above
A. Database
B. Application Server
C. User
D. None
Answer: A) Database
A. ANSI/SPARC architecture
B. Three-level architecture
C. Both A. and B.
D. None of the above
37. Requests and responses are transformed using ________ between different database layers.
A. Mapping
B. Deflection
C. Taping
D. Reflection
Answer: A) Mapping
38. For _____ mapping, the request must be transformed from external level to conceptual level.
A. External
B. Conceptual
C. Internal
D. Both A. and B.
39. DBMS transform requests from the conceptual to the internal level through ____ mapping.
A. External
B. Internal
C. Hidden
D. External / Conceptual
Answer: B) Internal
40. An important objective of a ___ is to make sure each user has a customized view of the same
data while only storing the underlying information once.
A. One-level architecture
B. Two-level architecture
C. Three-level architecture
D. Four-level architecture
41. By using three schemas, the database structure can be separated from the user's view. What's
the reason behind the same?
A. Internal
B. Conceptual
C. External
D. None
Answer: A) Internal
A. Access Paths
B. Storage Space Allocations
C. Data Compression
D. All of the above
45. __________ schemas describe the structure of a database from a conceptual perspective.
A. Internal
B. Conceptual
C. External
D. None
Answer: B) Conceptual
A. Physical
B. Logical
C. Rational
D. Transitional
Answer: B) Logical
A. Programmers
B. Database Administrators
C. Both A. and B.
D. None of the above
A. Physical
B. Logical
C. View
D. Rational
Answer: C) View
50. There are several schemas in a database that sometimes are referred to as ___________ on the
external level.
A. Schema
B. Superschema
C. Subschema
D. Adschema
Answer: C) Subschema
51. In view schemas, a particular _______ group can view only the database parts that are relevant
to them while hiding the rest of the database.
A. Admin
B. User
C. Employee
D. Team
Answer: B) User
52. _____ User interaction with a database system is described by the view schema.
A. End
B. Inter
C. Front
D. Back
Answer: A) End
53. ____________ are what connects the three types of schema within the DBMS.
A. Mapping
B. Copying
C. Layering
D. Breaking
Answer: A) Mapping
A. 1
B. 2
C. 3
D. 4 Answer: B) 2
5MARK:
10MARK
3
4
UNIT-III
Normalization of Database Tables: Database tables and Normalization – The Need for Normalization –
The Normalization Process – Higher level Normal Form. Introduction to SQL: Data Definition
Commands – Data Manipulation Commands – SELECT Queries – Additional Data Definition Commands
– Additional SELECT Query Keywords – Joining Database Tables.
DATABASE NORMALIZATION:
Database normalization is the process of structuring a relational database in accordance with a series of
so-called normal forms in order to reduce data redundancy and improve data integrity. It was first
proposed by British computer scientist Edgar F. Codd as part of his relational model.
Normalization entails organizing the columns (attributes) and tables (relations) of a database to ensure
that their dependencies are properly enforced by database integrity constraints. It is accomplished by
applying some formal rules either by a process of synthesis (creating a new database design)
or decomposition (improving an existing database design).
ensures thatconsistency,
Normalization
efficiency, theisdatabase
an important
and
is adaptable
accuracy.
processto
Itinchanging
makes
database
it easier
business
designtothat
manage
needs.
helpsand
improve
maintain
the database's
the data and
Database normalization is the process of organizing the attributes of the database to reduce or
eliminate data redundancy (having the same data but at different places).
Data redundancy unnecessarily increases the size of the database as the same data is repeated
in many places. Inconsistency problems also arise during insert, delete, and update
operations.
In the relational model, there exist standard methods to quantify how efficient a databases is.
These methods are called normal forms and there are algorithms to covert a given database
into normal forms.
Normalization generally involves splitting a table into multiple ones which must be linked
each time a query is made requiring data from the split tables.
Why do we need Normalization?
The primary objective for normalizing the relations is to eliminate the below anomalies. Failure
to reduce anomalies results in data redundancy, which may threaten data integrity and cause
additional issues as the database increases. Normalization consists of a set of procedures that
assist you in developing an effective database structure.
Insertion Anomalies: Insertion anomalies occur when it is not possible to insert data into a
database because the required fields are missing or because the data is incomplete. For
example, if a database requires that every record has a primary key, but no value is provided
for a particular record, it cannot be inserted into the database.
Deletion anomalies: Deletion anomalies occur when deleting a record from a database and
can result in the unintentional loss of data. For example, if a database contains information
about customers and orders, deleting a customer record may also delete all the orders
associated with that customer.
Updation anomalies: Updation anomalies occur when modifying data in a database and can
result in inconsistencies or errors. For example, if a database contains information about
employees and their salaries, updating an employee’s salary in one record but not in all
related records could lead to incorrect calculations and reporting.
Normalization Process in DBMS:
Database Normalization is any systematic process of organizing a database schema such that no data
redundancy occurs and there is least or no anomaly while performing any update operation on data. In
other words, it means dividing a large table into smaller pieces such that data redundancy should be
eliminated. The normalizing procedure depends on the functional dependencies among the attributes
inside a table and uses several normal forms to guide the design process.
The Normalization Process Begins With the Following:
First Normal Form (1NF): Ensures that each column contains only atomic values that cannot be
divided, and each record is unique.
Second Normal Form (2NF): Includes 1NF and removes subsets of data that would apply for more
than one row and places that data in a separate table. It deals with a partial dependency example when
a non-key attribute depends on part of a composite primary key.
Third Normal Form(3NF): This applies further normalization to the schema by removing transitive
dependencies, which are where the non-key attributes depend on other non-key attributes.
Besides these, BCNF is a stronger version of 3NF, developed by Raymond F. Boyce and Edgar F. Codd,
which handles some more cases of redundancy not dealt with by 3NF. Further normal forms like Fourth
Normal Form and Fifth Normal Form apply to the more complex scenarios for multi-valued dependencies
and join dependencies, respectively. These are also less commonly applied.
Boyce-Codd Normal Form (BCNF)
Boyce-Codd Normal Form, or BCNF for short, is an extension of the Third Normal Form, 3NF, in that it
seeks to eliminate certain types of redundancy that the latter does not catch. In BCNF, for a table to be
said to be in BCNF, it needs to have the following condition met: for any nontrivial functional
dependency
X→Y, X must be a superkey.
X needs to be a superkey. That is, for every functional dependency of the table, on the left of that
dependency, an attribute or set of attributes should be a superkey of the table.
This requirement is stronger than that of 3NF, which only had a non-key attribute non-transitively
dependent on a primary key. BCNF takes care of situations where, even for a table in 3NF, redundancy
would still arise whenever there is added a dependency whose left-hand side is not a superkey, hence
ensuring a higher level of normalization and, therefore, reduced redundancy.
First Normal A relation is in first normal form if every attribute in that relation is single-valued
Form (1NF) attribute.
A relation that is in First Normal Form and every non-primary-key attribute is fully
Second Normal functionally dependent on the primary key, then the relation is in Second Normal
Form (2NF) Form (2NF).
A relation is in the third normal form, if there is no transitive dependency for non-
prime attributes as well as it is in the second normal form. A relation is in 3NF if at
least one of the following conditions holds in every non-trivial function dependency
X –> Y.
Third Normal X is a super key.
Form (3NF) Y is a prime attribute (each element of Y is part of some candidate key).
Advantages of Normalization
Normalization eliminates data redundancy and ensures that each piece of data is stored in only one
place, reducing the risk of data inconsistency and making it easier to maintain data accuracy.
By breaking down data into smaller, more specific tables, normalization helps ensure that each table
stores only relevant data, which improves the overall data integrity of the database.
Normalization simplifies the process of updating data, as it only needs to be changed in one place
rather than in multiple places throughout the database.
Normalization enables users to query the database using a variety of different criteria, as the data is
organized into smaller, more specific tables that can be joined together as needed.
Normalization can help ensure that data is consistent across different applications that use the same
database, making it easier to integrate different applications and ensuring that all users have access
to accurate and consistent data.
Disadvantages of Normalization
Normalization can result in increased performance overhead due to the need for
additional join operations and the potential for slower query execution times.
Normalization can result in the loss of data context, as data may be split across multiple
tables and require additional joins to retrieve.
Proper implementation of normalization requires expert knowledge of database design
and the normalization process.
Normalization can increase the complexity of a database design, especially if the data
model is not well understood or if the normalization process is not carried out correctly.
Database normalization is a key concept in organizing data efficiently within a database.
By reducing redundancy, ensuring data consistency, and breaking data into well-structured
tables, normalization enhances the accuracy, scalability, and maintainability of a database.
It simplifies data updates, improves integrity, and supports flexible querying, making it an
essential practice for designing reliable and efficient database systems.
What is SQL?
SQL stands for Structured Query Language. It is a standardized programming language used
to manage and manipulate relational databases. It enables users to perform a variety of tasks
such as querying data, creating and modifying database structures, and managing access
permissions. SQL is widely used across various relational database management systems such
as MySQL, PostgreSQL, Oracle, and SQL Server
.
In this article, we will learn about what SQL is, and its functionality, structure, and practical
uses. We will also learn its characteristics, rules, commands, etc from basic to advanced
technical features.
SQL History
SQL was invented in 1970s and was first commercially distributed by Oracle.
The original name was given by IBM as Structured English Query Language,
abbreviated by the acronym SEQUEL.
Components of a SQL System
A SQL system consists of several key components that work together to enable efficient
data storage, retrieval, and manipulation. Understanding these components is crucial for
mastering SQL and its role in relational database systems. Some of the Key components of
a SQL System are:
Databases: Databases are structured collections of data organized into tables, rows, and
columns. Databases serve as repositories for storing information efficiently and provide
a way to manage and access data.
Tables: Tables are the fundamental building blocks of a database, consisting of rows
(records) and columns (attributes or fields). Tables ensure data integrity and consistency
by defining the structure and relationships of the stored information.
Queries: Queries are SQL commands used to interact with databases. They enable users
to retrieve, update, insert, or delete data from tables, allowing for efficient data
manipulation and retrieval.
Constraints: Constraints are rules applied to tables to maintain data
integrity. Constraints define conditions that data must meet to be stored in the database,
ensuring accuracy and consistency.
Stored Procedures: Stored procedures are pre-compiled SQL statements stored in the
database. Stored procedures can accept parameters, execute complex operations, and
return results, enhancing efficiency, reusability, and security in database management.
Transactions: Transactions are groups of SQL statements that are executed as a single
unit of work. Transactions ensure data consistency and integrity by allowing for the
rollback of changes if any part of the transaction fails.
Some other important components include:
Data Types
Indexes
Views
Security and Permissions
Joins
SQL Injection is a cyberattack where malicious SQL queries are injected into input fields
to manipulate a database, enabling unauthorized access, data modification, or corruption.
Using parameterized queries and input validation helps prevent such attacks.
Command Description
DROP Deletes an entire table, a view of a table, or other objects in the database
Command Description
Benefits of SQL
Efficiency: SQL is designed to handle complex queries and large datasets with optimal
performance, making data retrieval and manipulation seamless.
Standardization: As an ANSI and ISO standard language, SQL provides a universal
method to interact with relational databases across platforms.
Scalability: SQL supports databases ranging from small-scale applications to
enterprise-level systems, ensuring smooth operations regardless of size.
Flexibility: SQL can be extended with procedural programming (e.g., PL/SQL, T-SQL)
to build complex business logic and custom functions.
Limitations of SQL
Complexity in Advanced Operations: Advanced functionalities such as indexing,
query optimization, and performance tuning require in-depth technical knowledge.
Scalability Concerns: SQL performs best with structured data; handling unstructured
data or massive distributed systems can pose challenges.
Platform-Specific Variations: While SQL is standardized, many databases implement
unique extensions, leading to portability and compatibility issues.
SQL Use Cases
E-Commerce: Manage customer orders, product catalogs, and inventory.
Healthcare: Maintain patient records and appointment schedules.
Banking: Analyze transaction histories and generate financial reports.
Web Development: Power dynamic websites with user-specific content.
Machine Learning and Data Science: Combine SQL databases with tools like Python,
R, and TensorFlow to streamline machine learning workflows.
The select query in SQL is one of the most commonly used SQL commands to retrieve data from
a database. With the select command in SQL, users can access data and retrieve specific records
based on various conditions, making it an essential tool for managing and analyzing data.
In this article, we’ll learn the SQL SELECT statement syntax, show you SQL query examples,
and explore advanced techniques to help you use SELECT queries for data manipulation and
analysis.
Example:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
In this example, a new table called employees is created with columns for employee ID, first name, last
name, and hire date.
2. DQL - Data Query Language
DQL statements are used for performing queries on the data within schema objects. The purpose of
the DQL Command is to get some schema relation based on the query passed to it. This command
allows getting the data out of the database to perform operations with it. When a SELECT is fired
against a table or tables the result is compiled into a further temporary table, which is displayed or
perhaps received by the program.
DQL Command
Command Description Syntax
Table control
LOCK LOCK TABLE table_name IN lock_mode;
concurrency
Example:
INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');
This query inserts a new record into the employees table with the first name 'Jane', last
name 'Smith', and department 'HR'.
4. DCL - Data Control Language
DCL (Data Control Language) includes commands such as GRANT and REVOKE which mainly
deal with the rights, permissions, and other controls of the database system. These commands are
used to control access to data in the database by granting or revoking permissions.
Common DCL Commands
Example of DCL
GRANT SELECT, UPDATE ON employees TO user_name;
This command grants the user user_name the permissions to select and update records in
the employees table.
5. TCL - Transaction Control Language
Transactions group a set of tasks into a single execution unit. Each transaction begins with a
specific task and ends when all the tasks in the group are successfully completed. If any of
the tasks fail, the transaction fails. Therefore, a transaction has only two
results: success or failure. We can explore more about transactions here.
Common TCL Commands
Example:
BEGIN TRANSACTION;
UPDATE employees SET department = 'Marketing' WHERE department = 'Sales';
SAVEPOINT before_update;
UPDATE employees SET department = 'IT' WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
In this example, a transaction is started, changes are made, and a savepoint is set. If
needed, the transaction can be rolled back to the savepoint before being committed.
Most Important SQL Commands
There are also a few other SQL Commands we often rely on when writing powerful
queries. While they don’t fit neatly into the five main categories, they’re absolutely
essential for working with data effectively.
Command Description
TRUNCATE TABLE Removes all rows from a table but keeps its structure intact.
GROUP BY Groups rows that have the same values in specified columns.
JOIN Combines rows from two or more tables based on a related column.
Command Description
IN / BETWEEN /
Used for advanced filtering conditions.
LIKE
SQL commands such as DDL, DML, DCL, DQL, and TCL are foundational for
effective database management. From creating and modifying tables with DDL
commands to managing transactions with TCL commands in SQL, understanding each
type of command enhances our database skills. Whether we are manipulating data, or
managing data, SQL provides all sets of tools. Now, with this detailed guide, we hope you
have gained a deep understanding of SQL commands, their categories,
and syntax with examples.
ONE MARK:
A) 1NF
B) 2NF
C) 3NF
D) BCNF
Answer: B
A) Functional dependency
B) Atomicity of values
C) Multivalued dependency
D) Transitive dependency
Answer: B
A) Insertion anomaly
B) Deletion anomaly
C) Update anomaly
D) All of the above
Answer: D
A) 1NF
B) 2NF
C) 3NF
D) 4NF
Answer: C
A) 1NF only
B) 2NF and 3NF
C) Only 4NF
D) None
Answer: B
A) 2NF
B) BCNF
C) 1NF
D) All of the above
Answer: B
A) Reducing storage
B) Making queries fast
C) Eliminating anomalies
D) Increasing redundancy
Answer: C
A) 1NF
B) 2NF
C) 3NF
D) 4NF
Answer: D
A) Must be in 3NF
B) Must be in 1NF
C) Must be in 4NF
D) Must be denormalized
Answer: B
a) CREATE TABLE
b) MAKE TABLE
c) NEW TABLE
d) ADD TABLE
Answer: a) CREATE TABLE
a) To delete data
b) To select data
c) To insert data into a table
d) To update a table structure
Answer: c) To insert data into a table
a) DELETE TABLE
b) REMOVE TABLE
c) DROP TABLE
d) ERASE TABLE
Answer: c) DROP TABLE
a) Insert data
b) Update records
c) Retrieve data
d) Define tables
Answer: c) Retrieve data
a) ORDER BY
b) GROUP BY
c) WHERE
d) HAVING
Answer: c) WHERE
a) CHANGE
b) UPDATE
c) EDIT
d) MODIFY
Answer: b) UPDATE
a) UNIQUE
b) DISTINCT
c) REMOVE
d) SINGLE
Answer: b) DISTINCT
8. Which of the following is a valid SQL data type?
a) STRING
b) INTEGER
c) CHARACTERS
d) WORD
Answer: b) INTEGER
a) WHERE
b) ORDER BY
c) SORT BY
d) GROUP BY
Answer: b) ORDER BY
a) SUM()
b) TOTAL()
c) COUNT()
d) NUMBER()
Answer: c) COUNT()
13. Which clause groups rows that have the same values in specified columns?
a) GROUP
b) ORDER
c) GROUP BY
d) AGGREGATE
Answer: c) GROUP BY
14. What is the result of SELECT * FROM employees WHERE salary BETWEEN 30000 AND
50000;?
a) AS
b) RENAME
c) SET
d) ALIAS
Answer: a) AS
16. Which JOIN returns all matching records from both tables?
a) LEFT JOIN
b) INNER JOIN
c) RIGHT JOIN
d) OUTER JOIN
Answer: b) INNER JOIN
17. Which JOIN returns unmatched rows from the left table?
a) LEFT JOIN
b) RIGHT JOIN
c) FULL JOIN
d) SELF JOIN
Answer: a) LEFT JOIN
a) To update rows
b) To uniquely identify records
c) To delete records
d) To join tables
Answer: b) To uniquely identify records
a) INSERT COLUMN
b) MODIFY COLUMN
c) ADD COLUMN
d) CREATE COLUMN
Answer: c) ADD COLUMN
a) CREATE
b) ALTER
c) DELETE
d) DROP
Answer: c) DELETE
a) Adds rows
b) Deletes table
c) Changes table structure
d) Displays table
Answer: c) Changes table structure
a) 0
b) Empty string
c) Unknown or missing value
d) False
Answer: c) Unknown or missing value
a) ALLOW
b) GRANT
c) PERMIT
d) ENABLE
Answer: b) GRANT
a) Enforces uniqueness
b) Creates a new database
c) Establishes a relationship between tables
d) Deletes records
Answer: c) Establishes a relationship between tables
a) TOP
b) LIMIT
c) BOTH a and b (depending on the database)
d) NONE
Answer: c) BOTH a and b (depending on the database)
a) DELETE COLUMN
b) DROP COLUMN
c) REMOVE COLUMN
d) CLEAR COLUMN
Answer: b) DROP COLUMN
28. In which clause can aggregate functions like SUM, AVG be used directly?
a) SELECT
b) WHERE
c) ORDER BY
d) JOIN
Answer: a) SELECT
a) BETWEEN
b) LIKE
c) IN
d) MATCH
Answer: b) LIKE
a) FOREIGN KEY
b) PRIMARY KEY
c) UNIQUE
d) INDEX
Answer: a) FOREIGN KEY.
5 MARK:
10 MARK:
1. Discuss the different normal forms in detail from 1NF to BCNF. Explain the normalization
process with examples.?
2. Explain the importance of normalization in database design. How does it help in reducing
redundancy and anomalies? Illustrate with examples.?
3. Write detailed notes on SQL Data Definition Language (DDL) and Data Manipulation
Language (DML) commands with syntax and examples.?
4. Explain the SELECT statement in SQL. Discuss various clauses and keywords used in
SELECT queries with examples.?
5. Describe the different types of joins in SQL. Explain their syntax, use cases, and differences
using suitable examples.?
UNIT-IV
Advanced SQL:Relational SET Operators: UNION – UNION ALL – INTERSECT - MINUS.SQL Join
Operators: Cross Join – Natural Join – Join USING Clause – JOIN ON Clause – Outer Join.Sub Queries
and Correlated Queries: WHERE – IN – HAVING – ANY and ALL – FROM. SQL Functions: Date and
Time Function – Numeric Function – String Function – Conversion
ADVANCED SQL:
This basic to advanced SQL tutorial covers the entire SQL syllabus in a structured way and
provides the best learning material and strategies to master complete SQL in 30 Days. We have
laid out the complete SQL roadmap, and following this roadmap, you will learn all the concepts
of SQL.
All Important concepts in the SQL syllabus like SQL queries, syntax, datatypes, operators,
expressions, comments, etc will be explained in an easy way with examples to provide the best
learning experience. Be it your school/college exams or dream job interview, this tutorial will
help you get through it all. SQL are quite important for data analytics also if you wish to learn
data analytics and about sql and more tools that are used in the industry then you should
checkout our Complete Data analytics course.
SQL Overview:
Structured Query Language or SQL is a standard database language that is used to create,
maintain, destroy, update, and retrieve data from relational databases like MySQL, Oracle,
SQL Server, PostgreSQL, etc.
Whether you want to become a successful Data Scientist or a Business Analyst or a full-stack
web developer or even a software developer, learning and practicing SQL is very important for
all fields. Almost every big tech company uses SQL to manage and organize data be it Uber,
Netflix, Airbnb, Facebook, Google, LinkedIn, or any other, SQL is everywhere.
SQL is also the top and most popular language among data scientists or data engineers. Despite
lots of hype around NoSQL, Hadoop, and other technologies, it's one of the most-used
languages in the entire tech industry, and one of the most popular languages for developers of
all sorts.
Complete SQL Roadmap:
Now that we know, how much learning SQL is important or what is the significance of SQL in
today's world, we shall know how to learn SQL in a systematic way. To help you with this we
have created a practical SQL roadmap of 30 days , based on a lot of study and research.
This roadmap will give you a complete guideline with reference to learning and practicing
SQL for all types of job roles be it, Full Stack Developer, using SQL to retrieve information
from the database, or Data Scientist/Data Analyst using SQL to understand the dataset and then
analyzing it for different models.
Considering the depth of this topic, we would be requiring 30 days to learn SQL from the
beginning to the advanced level. After 30 days, you will be able to apply SQL to real-life
problems and crack the interviews.
Stay committed to the roadmap and solve as many questions as you can solve daily to maintain
consistency.
SET Operations in SQL
SQL supports few Set operations which can be performed on the table data. These are used to get
meaningful results from data stored in the table, under different special conditions.
In this tutorial, we will cover 4 different types of SET operations, along with example:
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION Operation:
UNION is used to combine the results of two or more SELECT statements. However it will
eliminate duplicate rows from its resultset. In case of union, number of columns and datatype
must be same in both the tables, on which UNION operation is being applied.
Notice how ‘Shawn Johnson’ and ‘Shane Johnson’ are present in each table before
the UNION operator is applied but are only listed ONCE in the result set.
UNION ALL
hese SELECT statements use the UNION ALL operator to combine the names of
‘Founders’ and ‘Employees’ into a single result set:
Notice how ‘Shawn Johnson’ and ‘Shane Johnson’ are present in each table before
the UNION ALL operator is applied and are duplicated in the result set.
INTERSECT
These SELECT statements use the INTERSECT operator to include values from the
‘Founders’ and ‘Employees’ tables that are listed in BOTH tables:
EXCEPT
\
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID"
in the "Customers" table. The relationship between the two tables above is the
"CustomerID" column.
Then, we can create the following SQL statement (that contains an INNER JOIN), that
selects records that have matching values in both tables:
Try it Yourself »
OrderID CustomerName
A Natural Join is a SQL join operation that merges rows from two tables based on the columns with
the same name and data type in both tables. The key feature of a natural join is that it automatically
finds the common columns between the tables and uses them to perform the join. In this article, we will
provide an in-depth overview of SQL Natural Join, the steps to implement it, and compare it
with Inner Join.
What is SQL Natural Join?
A Natural Join performs a join between two tables based on columns that have the same name and
compatible data types. This join operation automatically determines the common columns between the
tables and removes duplicates in the result set. The resulting table will contain only one copy of the
common columns, making the result concise and free from redundant data.
Syntax
SELECT *
FROM TABLE1
NATURAL JOIN TABLE2;
Features of Natural Join
Cartesian Product: Like other join operations, a natural join essentially combines every row from
one table with all rows from another table, but with additional conditions to match common
attributes.
Eliminates Duplicate Columns: If two tables share a column (or columns) with the same name, the
natural join will keep only one copy of the shared column(s), eliminating redundancy.
Automatic Column Matching: Natural joins automatically identify and match columns with the
same name and compatible data types. This eliminates the need to manually specify the columns in
the ON clause as with an INNER JOIN
Steps to implement SQL Natural Join
Let’s break down the process of performing a Natural Join through a practical example. We will create
two tables and join them using SQL Natural Join.
Step 1: Creating Database
First create a database named geeks:
create database geeks;
Step 2: Use the Database
Now, use the geeks database to create the tables:
use geeks;
Step 3: Create Tables
Let’s define two tables in the geeks database: department and employee.
1. Department Table
Create Table department (
DEPT_NAME Varchar(20),
MANAGER_NAME Varchar(255)
);
2. Employee Table
Create Table employee (
EMP_ID int,
EMP_NAME Varchar(20),
DEPT_NAME Varchar(255)
);
Step 4: Insert Data into the Tables
Let’s insert sample data into both the department and employee tables.
1. Insert data into the department table:
INSERT INTO department(DEPT_NAME,MANAGER_NAME) VALUES ( "IT", "ROHAN");
INSERT INTO department(DEPT_NAME,MANAGER_NAME) VALUES ( "SALES", "RAHUL");
INSERT INTO department(DEPT_NAME,MANAGER_NAME) VALUES ( "HR", "TANMAY");
INSERT INTO department(DEPT_NAME,MANAGER_NAME) VALUES ( "FINANCE",
"ASHISH");
INSERT INTO department(DEPT_NAME,MANAGER_NAME) VALUES ("MARKETING",
"SAMAY");
2. Insert data into the employee table.
INSERT INTO employee(EMP_ID, EMP_NAME, DEPT_NAME) VALUES (1, "SUMIT", "HR");
INSERT INTO employee(EMP_ID, EMP_NAME, DEPT_NAME) VALUES (2, "JOEL", "IT");
INSERT INTO employee(EMP_ID, EMP_NAME, DEPT_NAME) VALUES (3, "BISWA",
"MARKETING");
INSERT INTO employee(EMP_ID, EMP_NAME, DEPT_NAME) VALUES (4, "VAIBHAV", "IT");
INSERT INTO employee(EMP_ID, EMP_NAME, DEPT_NAME) VALUES (5, "SAGAR",
"SALES");
Step 5: Verify the Inserted Data
To ensure the data has been inserted correctly, run the following queries to view the data in both tables:
SELECT * FROM employee;
Output :
Query:
SELECT * FROM department;
Output :
Step 6: Perform SQL Natural Join
Now, we can perform the Natural Join between the employee and department tables. The join will be
based on the DEPT_NAME column, which is common to both tables.
Query:
SELECT *
FROM employee
NATURAL JOIN department;
Output :
Explanation: As shown, the DEPT_NAME column appears only once in the result, even
though it is present in both tables. The other columns from both tables are included, and we
now have a joined table that combines employee details with department information.
Difference Between Natural Join and Inner Join
While Natural Join and Inner Join are both used to combine data from two tables, they
differ in the following ways:
1. Natural Join:
Automatically joins tables based on common columns (with the same name and data
type).
Only one copy of each common column is included in the result.
No need to specify the joining condition explicitly.
2. Inner Join:
Requires you to explicitly specify the columns to join using the ON clause.
Includes all common columns from both tables in the result, which may lead to
duplication of column names.
Provides more control over the join condition.
In summary, a Natural Join is a powerful SQL feature that simplifies the process of
joining two tables based on common columns. It automatically handles the column
matching and removes duplicate columns, making it an efficient choice when working with
tables that share common attributes. However, for more complex joins or when you need
more control over the join conditions, an Inner Join might be a better choice.
What is the SQL USING Clause?
The USING clause is used in SQL JOIN statements to specify the column(s) that two tables
have in common. It eliminates the need to qualify column names with table aliases, making
queries more readable and concise.
Simplifies JOIN operations by matching common columns.
Ensures cleaner syntax by avoiding column qualification.
Works with INNER JOIN, LEFT JOIN, and RIGHT JOIN.
Syntax
SELECT column_list
FROM table1
JOIN table2
USING (common_column);
Key Terms
table1andtable2 are the tables we want to join.
common_columnis the shared column used for the join condition.
Example 1: Finding Employee Working Locations
The Employeestable contains information about employees, including
their EMPLOYEE_ID, LAST_NAME, and the department they belong to (DEPARTMENT_ID).
The Departments table lists department details, including DEPARTMENT_ID and LOCATION_ID.
Employee Table
Department Table
Query:
SELECT e.EMPLOYEE_ID, e.LAST_NAME, d.LOCATION_ID
FROM Employees e JOIN Departments d
USING(DEPARTMENT_ID);
Output:
The SQL USING clause is a valuable tool for simplifying JOIN operations by
reducing redundancy and improving query readability. By understanding its syntax, use
cases, and best practices, we can write cleaner, more efficient SQL queries and avoid
common pitfalls. It is especially useful when dealing with large databases, as it
helps reduce errors caused by column ambiguity. Leveraging the USING clause effectively
can enhance query performance and make our SQL code more maintainable in the long run.
SQL Outer Join
SQL Outer Joins allow retrieval of rows from two or more tables based on a related column.
Unlike inner Joins, they also include rows that do not have a corresponding match in one or both of the
tables. This capability makes Outer Joins extremely useful for comprehensive data analysis and
reporting, especially when dealing with incomplete data or wanting to show all records regardless of
matching conditions.
In this article, we will learn the concept of SQL Outer Joins, its types, syntax, and practical use cases.
We will also explore examples that show how Outer Joins can effectively solve real-world data retrieval
problems.
What is an SQL Outer Join?
Outer Join ensures that all rows from one or both tables are included in the result, even if there is no
match in the other table. It is particularly useful when you need to show all records from one table,
including those that don’t have a match in the other table.
Types of Outer Joins
There are three main types of Outer Joins in SQL:
LEFT OUTER JOIN (or LEFT JOIN)
RIGHT OUTER JOIN (or RIGHT JOIN)
FULL OUTER JOIN
Each of these join types handles unmatched rows differently, and understanding how they work will help
you use them effectively in your SQL queries.
Let’s Consider the two tables, Employees and Departments for understanding all the above outer join
with examples
Employees Table:
EmployeeID Name DepartmentID
1 John 101
2 Sarah 102
3 Michael –
4 Emma 103
Departments Table:
DepartmentID DepartmentName
101 HR
102 IT
103 Marketing
John 101 HR
Sarah 102 IT
Michael – –
In this example, Michael does not belong to any department, so the DepartmentName for
Michael is NULL.
RIGHT OUTER JOIN (RIGHT JOIN)
The RIGHT OUTER JOIN (often called RIGHT JOIN) returns all rows from the right
table, and the matching rows from the left table. If there is no match, the result will
include NULL values for columns from the left table.
Syntax:
SELECT table1.column1, table1.column2, table2.column1, ...
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Example: Let’s now look at a RIGHT OUTER JOIN on the Employees and Departments
tables. Suppose we want to retrieve all departments, even if no employees belong to a
specific department.
Query:
SELECT Employees.Name, Employees.DepartmentID, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentID DepartmentName
John 101 HR
Sarah 102 IT
Syntax:
SELECT table1.column1, table1.column2, table2.column1, ...
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Example: Let’s now use a FULL OUTER JOIN to get all employees and all departments,
regardless of whether an employee belongs to a department or a department has employees.
Query:
SELECT Employees.Name, Employees.DepartmentID, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Output:
Name DepartmentID DepartmentName
John 101 HR
Name DepartmentID DepartmentName
Sarah 102 IT
Michael – –
SQL Outer Joins are used for combining data from multiple tables while including unmatched rows.
Whether you need to retrieve all records from one table or both tables, outer joins offer a flexible way to
manage and analyze relational data. By understanding LEFT OUTER JOIN, RIGHT OUTER JOIN, and
FULL OUTER JOIN, you can optimize your SQL queries for complex data retrieval scenarios.
These joins allow you to handle incomplete data and generate comprehensive reports that include all
necessary records. Mastering Outer Joins is an essential skill for any SQL practitioner, whether you’re
working on business intelligence, data analysis, or general database management.
Nested Subqueries Versus Correlated Subqueries
With a normal nested subquery, the inner SELECT query runs first and executes once,
returning values to be used by the main query. A correlated subquery, however, executes
once for each candidate row considered by the outer query. In other words, the inner query is
driven by the outer query.
1. Fetching Data Based on Row-Specific Conditions
Correlated subqueries are often used when you need to filter data based on a condition that
involves comparing values from the outer query.
Example: Fetch Employees Who Earn More Than Their Department's Average Salary
SELECT last_name, salary, department_id
FROM employees outer
WHERE salary >
(SELECT AVG(salary)
FROM employees
WHERE department_id =
outer.department_id group by department_id);
Explanation:
In this example, the subquery calculates the average salary for each department, and for each
employee, the outer query checks if their salary is greater than the average for their
department.
2. Using Correlated Subqueries with UPDATE
Correlated subqueries can also be used with UPDATE statements to modify data based on
related information from another table.
Example: Update Employee Salary Based on Department Average
UPDATE employees e
SET salary = (SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id)
WHERE department_id = 101;
Explanation:
This query updates the salaries of employees in department 101 based on the average salary
for that department. The subquery is evaluated for each row in the employees table.
3. Using Correlated Subqueries with DELETE
We can use a correlated subquery within a DELETE statement to remove rows from one
table based on conditions in another table.
Example: Delete Employees Who Do Not Belong to Department 101
DELETE FROM table1 alias1
WHERE column1 operator
(SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
Explanation:
This query deletes employees who do not belong to the 'HR' department by using a correlated
subquery in the NOT EXISTS clause.
4. Using EXISTS with Correlated Subqueries
The EXISTS operator is often used in correlated subqueries to test if a subquery returns any
rows. It returns TRUE if the subquery has at least one row.
Example: Find Employees Who Have at Least One Person Reporting to Them
SELECT employee_id, last_name, job_id, department_id
FROM employees outer
WHERE EXISTS ( SELECT ’X’
FROM employees
WHERE manager_id =
outer.employee_id);
Output
5. Using NOT EXISTS with Correlated Subqueries
The NOT EXISTS operator is used to check if a subquery does not return any rows. This is
useful for finding records that do not match specific criteria.
Example: Find Departments Without Any Employees
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (SELECT ’X’
FROM employees
WHERE department_id
= d.department_id);
Output
Explanation:
This query retrieves departments that do not have any employees assigned to them.
The NOT EXISTS clause ensures that only those departments without employees are
selected.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Demo Database
Below is a selection from the "Customers" table in the Northwind sample database:
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D
Output:
This query retrieves all product names from the Products table because TRUE always
evaluates as true for every row.
Example 2: Retrieve product names if all records in the OrderDetails table
have a quantity of 6 or 2.
SELECT ProductName
FROM Products
WHERE ProductID = ALL (SELECT ProductID
FROM OrderDetails
WHERE Quantity = 6 OR Quantity = 2);
Output:
This query ensures that the product names returned have ALL quantities of 6 or 2 in the
OrderDetails table.
Example 3 : Find the OrderIDs where the maximum quantity in the order
exceeds the average quantity of all orders.
SELECT OrderID
FROM OrderDetails
GROUP BY OrderID
HAVING MAX(Quantity) > ALL (SELECT AVG(Quantity)
FROM OrderDetails
GROUP BY OrderID);
Output:
ANY
This query filters out OrderIDs where the maximum quantity is greater than the average
quantity of the orders.
What is the SQL ANY Operator?
ANY compares a value to each value in a list or results from a query and evaluates to true
if the result of an inner query contains at least one row.
ANY return true if any of the subqueries values meet the condition.
ANY must be preceded by comparison operators.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name comparison_operator ANY
(SELECT column_name
FROM table_name
WHERE condition(s));
How to Use SQL ANY with SELECT, WHERE, and HAVING
Example 1 : Find distinct category IDs of products that appear in the
OrderDetails table.
Query:
SELECT DISTINCT CategoryID
FROM Products
WHERE ProductID = ANY (SELECT ProductID
FROM OrderDetails);
Output:
This query finds the distinct CategoryIDs of products that exist in the OrderDetails table.
Example 2 : Find product names with a quantity of 9 in the OrderDetails
table.
Query:
SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID
FROM OrderDetails
WHERE Quantity = 9);
Output:
product names
This query retrieves product names where at least one record in the OrderDetails table has
a quantity of 9.
Differences Between SQL ALL and ANY
ALL requires that the condition be true for every value in the subquery result, while
ANY only needs the condition to be true for at least one value in the subquery.
ALL is used when you want to compare a value against all values in the subquery, while
ANY is useful when you want to compare a value against any one of the values.
The ALL and ANY operators in SQL are essential for advanced filtering based on dynamic
conditions. The key differences lie in how they evaluate the conditions across the results of
the subquery. ALL requires the condition to be true for all values in the subquery, while
ANY only needs the condition to be true for one value. By mastering these operators, you
can write more efficient and flexible SQL queries.
SQL FUNCTION:
SQL Functions are built-in programs that are used to perform different operations on the
database.
There are two types of functions in SQL:
Aggregate Functions
Scalar Functions
SQL Aggregate Functions
SQL Aggregate Functions operate on a data group and return a singular output. They are
mostly used with the GROUP BY clause to summarize data.
Some common Aggregate functions with Syntax and description are shown in the table
below.
Aggregate
Function Description Syntax
SELECT COUNT(column_name)
Counts the number of rows
COUNT() FROM table_name
80
90
85
95
50
400
Scalar Functions Examples
Let's look at some examples of each Scalar Function in SQL.
UCASE() Function Example
Converting names of students from the table Students to uppercase.
Query:
SELECT UCASE(NAME) FROM Students;
Output:
NAME
HARSH
SURESH
PRATIK
DHANRAJ
RAM
harsh
suresh
pratik
dhanraj
ram
HARS
SURE
PRAT
DHAN
RAM
90
50
80
95
85
HARSH 2017-01-13
SURESH 2017-01-13
PRATIK 2017-01-13
DHANRAJ 2017-01-13
RAM 2017-01-13
Handling date and time data in MySQL is essential for many database operations, especially when it
comes to handling timestamps, scheduling tasks, or generating time-based. MySQL provides a variety
of date and time functions that help users work with date values, perform calculations, and format them
as needed.
These functions allow developers to perform calculations, extract specific parts of a date, or even
format the output for better readability. In this article, we will explore the most commonly used
MySQL date functions, explain their syntax, provide examples, and walk you through how to
effectively use them
Query:
SELECT Name, DATE(BirthTime)
AS BirthDate FROM Test;
Output:
Name BirthDate
Pratik 1996-09-26
Pratik 26
Pratik 1996
Pratik 581
STRING FUNCTION:
SQL string functions are used primarily for string manipulation. The following table
details the important string functions −
ASCII()
1
Returns numeric value of left-most character
CHAR()
2
Returns the character for each integer passed
CHARINDEX()
3
Returns the position of a substring within the given string.
CONCAT_WS()
4
Returns concatenate with separator
CONCAT()
5
Returns concatenated string
DIFFERENCE()
6
returns an integer value measuring the difference between the SOUNDEX() values of two different
expressions(strings).
ESCAPE()
7
Returns a text with escaped characters.
FORMAT()
8
Returns the formatted string.
LEFT()
9
Returns the extracting string.
LEN()
10
Returns the length of the given string.
LOWER()
11
Returns returns the lower case of the given string.
LTRIM()
12
Returns a string after removing all the white spaces and characters from the string found on the left side.
NCHAR()
13
Returns the Unicode character based on the number code.
PATINDEX()
14
Returns the position of a pattern in a string.
QUOTENAME()
15
Returns a string with a delimiter
REPLACE()
16
Returns a new string by replacing all the occurrences of the particular part of a string (substring) with a
specified string.
REPLICATE()
17
Returns the repeated value.
REVERSE()
18
Returns a reversed string.
RIGHT()
19
Returns the rightmost characters from the actual(current) string.
RTRIM()
20
Returns a string after removing all trailing blanks.
SOUNDEX()
21
Returns the Soundex string.
SPACE()
22
returns a string consisting of N number of space characters.
STR()
23
Returns a number as string.
STRING_AGG()
24
Concatenates the values of string expressions and places separator values between them.
STRING_SPLIT()
25
Splits a string into rows of substrings.
STUFF()
26
Returns a new string by inserting the second expression at the specified deleted place.
SUBSTRING()
27
Returns the part of the character.
TRANSLATE()
28
Returns a string from the first argument.
TRIM()
29
Returns a trimmed string.
UNICODE()
30
Returns an integer value of the first character.
UPPER()
31
returns a string that has all the characters in upper case.
.No. Function & Description
ABS()
1
Returns the absolute value of numeric expression.
ACOS()
2
Returns the arccosine of numeric expression. Returns NULL if the value is not in the range -1 to 1.
ASIN()
3
Returns the arcsine of numeric expression. Returns NULL if value is not in the range -1 to 1
ATAN()
4
Returns the arctangent of numeric expression.
ATN2()
5
Returns the arctangent of the two variables passed to it.
CEILING()
6
Returns the smallest (closest to negative infinity) integer value that is greater than or equal to this value.
COS()
7
Returns the trigonometric cosine of the given value.
COT()
8
Returns the trigonometric cotangent of the given value.
DEGREES()
9
Returns numeric expression converted from radians to degrees.
EXP()
10
Returns the base of the natural logarithm (e) raised to the power of passed numeric expression.
FLOOR()
11
Returns the largest integer value that is not greater than passed numeric expression.
LOG()
12
Returns the natural logarithm of the passed numeric expression.
LOG10()
13
Returns the base-10 logarithm of the passed numeric expression.
PI()
14
Returns the value of pi
POWER()
15
Returns the value of one expression raised to the power of another expression
RADIANS()
16
Returns the value of passed expression converted from degrees to radians.
RAND()
17
Returns the random value between 0 and 1.
ROUND()
18 Returns numeric expression rounded to an integer. Can be used to round an expression to a number of
decimal points
SIGN()
19
Returns the sign of a number, indicating whether it is positive, negative, or zero.
SIN()
20
Returns the sine of numeric expression given in radians.
SQRT()
21
Returns the non-negative square root of numeric expression.
TAN()
22
Returns the tangent of numeric expression expressed in radians.
ONE MARK:
1. What does the UNION operator do in SQL?
a) Combines results and removes duplicates
b) Combines results with duplicates
c) Returns only distinct values from one table
d) Returns all null values
Answer: a
3. Which SQL operator returns common rows between two SELECT statements?
a) UNION
b) INTERSECT
c) MINUS
d) JOIN
Answer: b
4.Which SQL operator returns rows from the first query that are not in the second?
a) UNION
b) INTERSECT
c) MINUS
d) JOIN
Answer: c
5.What condition must be met to use set operators like UNION or INTERSECT?
a) Same number of columns
b) Same data types in corresponding columns
c) Both a and b
d) No conditions
Answer: c
6. Which join returns all possible combinations of rows from two tables?
a) Inner Join
b) Left Join
c) Cross Join
d) Natural Join
Answer: c
7. Which join matches columns with the same name in both tables automatically?
a) Cross Join
b) Inner Join
c) Natural Join
d) Left Join
Answer: c
8. The USING clause is used in which type of join?
a) Full Outer Join
b) Join with common column name
c) Cross Join
d) Join without condition
Answer: b
9. Which join condition specifies the exact columns to join on?
a) USING clause
b) ON clause
c) WHERE clause
d) GROUP BY clause
Answer: b
10. Which join includes all rows from the left table and matching rows from the right?
a) Inner Join
b) Left Outer Join
c) Right Outer Join
d) Full Outer Join
Answer: b
11. Which join includes only rows with matching values in both tables?
a) Outer Join
b) Inner Join
c) Cross Join
d) Left Join
Answer: b
12. Which join returns all records from both tables when there is a match?
a) Inner Join
b) Right Join
c) Full Outer Join
d) Self Join
Answer: c
13. Which clause is best used to specify multiple join conditions?
a) WHERE
b) USING
c) ON
d) GROUP BY
Answer: c
🔹 SQL Functions
23. Which function returns the current system date and time?
a) GETDATE()
b) SYSDATE
c) NOW()
d) All of the above
Answer: d
24. Which function extracts year from a date?
a) EXTRACT(YEAR FROM date)
b) GETYEAR(date)
c) YEAROF(date)
d) YEAR()
Answer: a
25. Which function returns the difference between two dates?
a) DATEDIFF()
b) DATE_SUB()
c) DIFF()
d) TIME_DIFF()
Answer: a
📆 Numeric Functions
📆 String Functions
📆 Conversion Functions
5MARK:
1.Explain the differences between UNION and UNION ALL with examples.?
4. Explain the use of SQL functions with examples from each category: Date-Time, Numeric, String, and
Conversion.?
5. Differentiate between WHERE, HAVING, IN, ANY, and ALL clauses in SQL with examples.?
10 MARK:
1.Compare and contrast the SQL set operators: UNION, UNION ALL, INTERSECT, and MINUS.
Provide examples.?
2. Explain all types of SQL JOINs: CROSS JOIN, NATURAL JOIN, JOIN USING, JOIN ON, and
OUTER JOIN with suitable examples.?
3. Write and explain various subqueries, including correlated subqueries. How do WHERE, IN, ANY,
ALL, HAVING, and FROM clauses use subqueries?
4. Describe various SQL functions: Date and Time, Numeric, String, and Conversion functions. Provide
practical examples.?
5. Design a complex SQL query involving JOINs, subqueries, set operators, and SQL functions. Explain
the logic of the query.?
UNIT-IV COMPLETED
UNIT-V
PL/SQL Introduction
What is PL/SQL?
PL/SQL is a combination of SQL and procedural programming constructs, enabling developers to
write code that performs database operations efficiently. It was developed by Oracle to enhance
SQL’s capabilities and allow for advanced error handling, complex calculations, and programmatic
control over database operations.
PL/SQL allows developers to:
Execute SQL queries and DML commands inside procedural blocks.
Define variables and perform complex calculations.
Create reusable program units, such as procedures, functions, and triggers.
Handle exceptions, ensuring the program runs smoothly even when errors occur
Key Features of PL/SQL
PL/SQL brings the benefits of procedural programming to the relational database world. Some of the
most important features of PL/SQL include:
1. Block Structure: PL/SQL can execute a number of queries in one block using single command.
2. Procedural Constructs: One can create a PL/SQL unit such as procedures, functions, packages,
triggers, and types, which are stored in the database for reuse by applications.
3. Error Handling: PL/SQL provides a feature to handle the exception which occurs in PL/SQL block
known as exception handling block.
4. Reusable Code: Create stored procedures, functions, triggers, and packages, which can be executed
repeatedly.
5. Performance: Reduces network traffic by executing multiple SQL statements within a single block
Differences Between SQL and PL/SQL
SQL PL/SQL
Feature
Data Performs actions directly on the Can contain SQL inside its blocks and is
Handling database. used for more control over data handling
Typically, each block performs a logical action in the program. A block has the following structure:
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
PL/SQL code is written in blocks, which consist of three main sections:
Declare section starts with DECLARE keyword in which variables, constants, records as cursors
can be declared which stores data temporarily. It basically consists definition of PL/SQL identifiers.
This part of the code is optional.
Execution section starts with BEGIN and ends with END keyword.This is a mandatory section and
here the program logic is written to perform any task like loops and conditional statements. It
supports all DML commands, DDL commands and SQL*PLUS built-in functions as well.
Exception section starts with EXCEPTION keyword.This section is optional which contains
statements that are executed when a run-time error occurs. Any exceptions can be handled in this
section.
PL/SQL Identifiers
In PL/SQL, identifiers are names used to represent various program elements like variables, constants,
procedures, cursors, triggers etc. These identifiers allow you to store, manipulate, and access data
throughout your PL/SQL code.
1. Variables in PL/SQL
Like several other programming languages, variables in PL/SQL must be declared prior to its use. A
variable is like a container that holds data during program execution. Each variable must have a valid
name and a specific data type.
Syntax for declaration of variables:
variable_name datatype [NOT NULL := value ];
variable_name: The name of the variable.
datatype: The data type of the variable (e.g., INTEGER, VARCHAR2).
NOT NULL: This optional constraint means the variable cannot be left empty.
:= value: This optional assignment assigns an initial value to the variable.
Example: Declaring Variables
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed.
Explanation:
SET SERVEROUTPUT ON: It is used to display the buffer used by the dbms_output.
var1 INTEGER : It is the declaration of variable, named var1 which is of integer type. There are
many other data types that can be used like float, int, real, smallint, long etc. It also supports
variables used in SQL as well like NUMBER(prec, scale), varchar, varchar2 etc.
Slash (/) after END;: The slash (/) tells the SQL*Plus to execute the block.
Assignment operator (:=) : It is used to assign a value to a variable.
2. Displaying Output in PL/SQL
The outputs are displayed by using DBMS_OUTPUT which is a built-in package that enables the user
to display output, debugging information, and send messages from PL/SQL blocks, subprograms,
packages, and triggers. Let us see an example to see how to display a message using PL/SQL :
Example: Displaying Output
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
var varchar2(40) := 'I love GeeksForGeeks' ;
BEGIN
dbms_output.put_line(var);
END;
/
Output:
I love GeeksForGeeks
/*
This is a multi-line comment
that spans over multiple lines.
*/
4. Taking input from users
In PL/SQL we can take input from the user and store it in a variable using substitution variables.
These variables are preceded by an & symbol. Let us see an example to show how to take input from
users in PL/SQL:
Example: Taking Input from Users
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
BEGIN
null;
END;
/
Output:
Enter value for a: 24
old 2: a number := &a;
new 2: a number := 24;
Enter value for b: 'GeeksForGeeks'
old 3: b varchar2(30) := &b;
new 3: b varchar2(30) := 'GeeksForGeeks';
BLOCK STRUCTURE:
In PL/SQL, All statements are classified into units that is called Blocks. PL/SQL blocks can include
variables, SQL statements, loops, constants, conditional statements and exception handling. Blocks can
also build a function or a procedure or a package.
The Declaration section: Code block start with a declaration section, in which memory variables,
constants, cursors and other oracle objects can be declared and if required initialized.
The Begin section: Consist of set of SQL and PL/SQL statements, which describe processes that have
to be applied to table data. Actual data manipulation, retrieval, looping and branching construct s are
specified in this section.
The Exception section: This section deals with handling errors that arise during execution data
manipulation statements, which make up PL/SQL code block. Errors can arise due to syntax, logic
and/or validation rule.
The End section: This marks the end of a PL/SQL block.
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database
applications. Variables act as placeholders for data which enable us to manipulate and store information
within our PL/SQL programs.
Here, we will explore various methods of declaring variables in PL/SQL, including syntax, examples, and
practical use cases. We will cover variable initialization, scope, and the use of variable attributes
like %TYPE and %ROWTYPE.
How to Declare PL/SQL Variables?
When writing PL/SQL code it is important to declare variables properly to store and manipulate data
effectively. Variables act as containers for values and enable various operations on the stored data.
Variables in PL/SQL are declared using the DECLARE keyword within an anonymous block or a named
program unit such as a procedure, function, or package.
Common Methods for Declaring Variables in PL/SQL
The below methods are used to declare a variable in PL/SQL are as follows:
Table of Content
Using Declare Variables in PL/SQL
Using Initializing Variables in PL/SQL
Using Variable Scope in PL/SQL
Using Variable Attributes
Let's understand each method one be one along with the Examples.
1. Using Declare Variables in PL/SQL
To declare a variable in PL/SQL, use the DECLARE keyword followed by the variable name and its data
type. Optionally, you can also assign an initial value to the variable using the ':=' operator.
Syntax:
DECLARE
variable_name datatype := initial_value;
here,
variable_name: It is the name of the variable.
datatype: It is the data type of the variable.
:= initial_value: It is an optional assignment of an initial value to the variable.
Example:
DECLARE
name VARCHAR2(20) := 'GeeksForGeeks';
BEGIN
DBMS_OUTPUT.PUT_LINE(name);
END;
Output:
Output
Explanation: In the above Query, We have declares a variable named "name" with a size
of 20 characters and initializes it with the value GeeksForGeeks then prints the value of the variable
using DBMS_OUTPUT.PUT_LINE.
2. Using Initializing Variables in PL/SQL
In this method Variables can be initialized in two ways either during declaration or later in the code.
a. Initializing during declaration
Variables can be assigned values when declared, as shown below:
Syntax:
DECLARE
my_variable NUMBER := value;
BEGIN
-- PL/SQL code
END;
Example:
DECLARE
name VARCHAR2(20) := 'GeeksForGeeks';
BEGIN
DBMS_OUTPUT.PUT_LINE(name);
END;
Output:
Explanation: In the above Query, We have declares a variable salary_var with the same data type as
the salary column in the employees table, assigns a value of 70000 to it, and then prints the assigned
salary using DBMS_OUTPUT.PUT_LINE.
2. Using %ROWTYPE Attribute
In this example, We have declared a record variable employee_record using %ROWTYPE to match the
structure of the employees table and fetched the data from the employees table into
the employee_record variable using a SELECT INTO statement then we have displayed the retrieved
data from the employee_record using DBMS_OUTPUT.PUT_LINE.
DECLARE
employee_record employees%ROWTYPE;
BEGIN
-- Fetch data from the table into the record variable
SELECT * INTO employee_record FROM employees WHERE employee_id = 2;
Explanation: In the above Query, We have declares a record variable employee_record that matches the
structure of the employees table. It then fetches the data for the employee with employee_id 2 into
the employee_record variable using a SELECT INTO statement and prints the retrieved data
using DBMS_OUTPUT.PUT_LINE.
PL/SQL Operator
The PL/SQL language offers various operators for data manipulation and logical processing. There are
several types of these operators which include arithmetic operators, relational operators,
comparison operators, and logical operators. In this guide, we will learn about the various PL/SQL
operators with the help of examples and so on.
PL/SQL Operators
PL/SQL operators are used to operate on variables, constants, or expressions in PL/SQL blocks.
They enable us to process data through arithmetic, relational or logical operations that is calculations,
comparisons, or logical decisions. Operators are essential in PL/SQL for performing calculations and
making decisions, from simple arithmetic to complex logic.
Types of PL/SQL Operators
PL/SQL offers several types of operators, each serving a unique purpose:
1. Arithmetic Operators
Arithmetic operators in PL/SQL are used to perform basic mathematical operations such as addition,
subtraction, multiplication, division, and modulus.
Operator Description Example
+ Addition 10+5
- Subtraction 10-5
* Multiplication 10*5
/ Division 10/5
Example:
DECLARE
num1 NUMBER := 20;
num2 NUMBER := 4;
result NUMBER;
BEGIN
result := num1 + num2;
DBMS_OUTPUT.PUT_LINE('Addition Result: ' || result);
result := num1 - num2;
DBMS_OUTPUT.PUT_LINE('Subtraction Result: ' || result);
result := num1 * num2;
DBMS_OUTPUT.PUT_LINE('Multiplication Result: ' || result);
result := num1 / num2;
DBMS_OUTPUT.PUT_LINE('Division Result: ' || result);
result := MOD(num1, num2);
DBMS_OUTPUT.PUT_LINE('Modulus Result: ' || result);
END;
2. Relational Operators
Comparison operators compare two values and return a Boolean result (TRUE, FALSE, or NULL).
These operators are commonly used in conditional statements such as IF and LOOP.
Operator Description Example
= Equal to x=y
Example:
DECLARE
age1 NUMBER := 25;
age2 NUMBER := 30;
BEGIN
IF age1 <> age2 THEN
DBMS_OUTPUT.PUT_LINE('Ages are not equal.');
END IF;
IF age1 < age2 THEN
DBMS_OUTPUT.PUT_LINE('Age1 is less than Age2.');
END IF;
END;
3. Logical Operators
Logical operators are used to combine or negate conditions, and they evaluate to a Boolean value.
These operators are often used in IF, CASE, and LOOP statements.
Operator Description Examples
AND Returns TRUE if both conditions are TRUE x > 5 AND y < 10
Example:
DECLARE
score1 NUMBER := 80;
score2 NUMBER := 70;
BEGIN
IF score1 >= 75 AND score2 >= 75 THEN
DBMS_OUTPUT.PUT_LINE('Both scores are above average.');
ELSIF score1 >= 75 OR score2 >= 75 THEN
DBMS_OUTPUT.PUT_LINE('At least one score is above average.');
ELSE
DBMS_OUTPUT.PUT_LINE('Both scores are below average.');
END IF;
END;
4. Comparison Operators
These operators are used to compare values and return TRUE or FALSE based on the condition.
Operator Description Examples
LIKE Matches a value against a pattern name LIKE 'A%' (names starting with 'A')
Example:
DECLARE
student_name VARCHAR2(50) := 'Alice';
student_age NUMBER := 19;
BEGIN
IF student_name LIKE 'A%' THEN
DBMS_OUTPUT.PUT_LINE('Student name starts with "A".');
END IF;
PL/SQL operators are powerful tools for building conditions, performing calculations, and
manipulating data effectively. Handling arithmetic operations, logical decisions, or complex
comparisons, understanding these operators is essential for efficient PL/SQL programming. Mastery of
these operators can significantly improve the robustness and flexibility of your database applications.
Control Structures are just a way to specify flow of control in programs. Any algorithm or program
can be more clear and understood if they use self-contained modules called as logic or control
structures. It basically analyzes and chooses in which direction a program flows based on certain
parameters or conditions. There are three basic types of logic, or flow of control, known as:
1. Sequential Logic (Sequential Flow) Sequential logic as the name suggests follows a serial or
sequential flow in which the flow depends on the series of instructions given to the computer.
Unless new instructions are given, the modules are executed in the obvious sequence. The
sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order
in which modules are written. Most of the processing, even some complex problems, will generally
3. Iteration Logic (Repetitive Flow) The Iteration logic employs a loop which involves a repeat
statement followed by a module known as the body of a loop. The two types of these structures are:
Repeat-For Structure This structure has the form:
Repeat for i = A to N by I:
[Module]
[End of loop]
Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B.
K increases or decreases according to the positive and negative value of I respectively.
Repeat-For FlowImplementation:
PL/SQL, Oracle's procedural extension of SQL, offers powerful capabilities for
managing and processing data within the database. One of its key features is
support for complex data structures, including collections like nested tables. A
nested table in PL/SQL is a dynamic, array-like data structure that allows you to
store multiple elements of the same data type in a single variable. Every PL/SQL
query contains BEGIN, and END keywords. We can nest blocks in PL/SQL. It
supports anonymous blocks and named blocks. Anonymous blocks are not
stored in the database while named blocks are stored.
Here, we’ll explore nested tables in PL/SQL. We’ll look at what they are, how they
work, and what they can be used for.
PL/SQL block Structure
A typical PL/SQL block has the following structure:
-- Declaration Section
DECLARE
-- Variable and nested table declarations
BEGIN
-- Execution Section
-- Statements to perform operations
EXCEPTION
-- Exception Handling Section
-- Optional: Handle any runtime errors
END;
What are PL/SQL Nested Tables?
Nested tables in PL/SQL are data structures that function like arrays, allowing you to store multiple
values in a single column. They are stored in a database. However, unlike traditional arrays, nested tables
in PL/SQL are dynamic i.e. the amount of memory utilized can increase or decrease as per use. They
allow you to work on variable amounts of data dynamically. They are used when an unknown number of
elements are to be stored. Elements of any datatype can be stored in a nested table.
PL/SQL nested tables are stored in the database and can hold elements of any data type. They are
particularly useful when dealing with complex data structures and dynamic datasets. Two main types of
nested tables exist:
Bounded Nested Tables: Have a fixed size.
Unbounded Nested Tables: Are dynamic and can grow or shrink based on usage
Syntax of PL/SQL nested tables
DECLARE
--DECLARE THE NESTED TABLE TYPE
TYPE nested_table_type IS TABLE OF data_type;
--Declare variable of the nested table type
variable nested_table_type;
BEGIN
--initialize the variable
variable := nested_table_type( element1,element2 .....elementn)
-- Perform the required operations
-- free up the allocated memory
variable : =NULL;
END;
The nested table type is declared using the TYPE keyword.
The variable is created using IS TABLE OF.
Working with PL/SQL Nested Tables
1. Declaring a Nested Table Variable
A nested table in oracle is declared in the declaration section like cursors or procedure. The datatype is
declared along with the name of the nested table.
TYPE nested_table_type IS TABLE OF data_type;
variable nested_table_type;
2. Initializing a Nested Table
When a variable is declared using the nested table type it is initialized to NULL. Constructor initializes
the value to the variable.
Syntax:
variable_of_nested_table := nested_table_type();
Arithmetic Operators in PL/SQL
In PL/SQL (Oracle's Procedural Language extension to SQL), arithmetic operators are used to
perform calculations in variable assignments, expressions, and control structures.
+ Addition x := 10 + 5; 15
- Subtraction x := 10 - 5; 5
* Multiplication x := 10 * 5; 50
/ Division x := 10 / 5; 2.0
** Exponentiation x := 2 ** 3; 8
📌 PL/SQL uses ** for exponentiation (unlike standard SQL which doesn't support
exponentiation directly).
Output
makefile
CopyEdit
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Exponentiation: 100000
Control Structures and Embedded SQL in PL/SQL
PL/SQL enhances SQL by adding control structures (like loops, conditionals) and allows
embedded SQL (using SQL commands inside PL/SQL code). This makes PL/SQL powerful for
creating procedures, triggers, and scripts.
A. Conditional Statements
IF-THEN
plsql
CopyEdit
IF salary > 5000 THEN
bonus := 1000;
END IF;
IF-THEN-ELSE
plsql
CopyEdit
IF salary > 5000 THEN
bonus := 1000;
ELSE
bonus := 500;
END IF;
IF-THEN-ELSIF
plsql
CopyEdit
IF salary > 10000 THEN
bonus := 2000;
ELSIF salary > 5000 THEN
bonus := 1000;
ELSE
bonus := 500;
END IF;
B. Loops
Simple Loop
plsql
CopyEdit
LOOP
total := total + 1;
EXIT WHEN total = 10;
END LOOP;
WHILE Loop
plsql
CopyEdit
WHILE total < 10 LOOP
total := total + 1;
END LOOP;
FOR Loop
plsql
CopyEdit
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
END LOOP;
INSERT
plsql
CopyEdit
BEGIN
INSERT INTO employees (empno, ename)
VALUES (1002, 'John');
END;
UPDATE
plsql
CopyEdit
BEGIN
UPDATE employees
SET salary = salary + 1000
WHERE empno = 1002;
END;
DELETE
plsql
CopyEdit
BEGIN
DELETE FROM employees
WHERE empno = 1002;
END;
In PL/SQL, a nested block means placing one PL/SQL block inside another. This is useful for
modularizing your code, isolating variables, and handling errors locally.
plsql
CopyEdit
DECLARE
outer_var NUMBER := 10;
BEGIN
DBMS_OUTPUT.PUT_LINE('Outer Block Start');
DECLARE
inner_var NUMBER := 5;
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner Block');
DBMS_OUTPUT.PUT_LINE('Sum = ' || (outer_var + inner_var));
END;
🔹 Output:
mathematica
CopyEdit
Outer Block Start
Inner Block
Sum = 15
Outer Block End
Scope Inner blocks can access variables from outer blocks, but not vice versa.
Isolation Inner blocks can have variables with the same name (shadowing outer ones).
Feature Description
Local Handling Each block can have its own EXCEPTION section.
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner Block');
x := x / 0; -- causes division by zero
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Error: Division by zero in Inner Block');
END;
🔹 Output:
mathematica
CopyEdit
Outer Block
Inner Block
Error: Division by zero in Inner Block
After Inner Block
PL/SQL enhances SQL with procedural capabilities like variables, control structures, and error
handling.
plsql
CopyEdit
BEGIN
INSERT INTO employees (empno, ename, salary)
VALUES (1001, 'John', 5000);
DBMS_OUTPUT.PUT_LINE('Record Inserted');
END;
plsql
CopyEdit
BEGIN
UPDATE employees
SET salary = salary + 1000
WHERE empno = 1001;
DBMS_OUTPUT.PUT_LINE('Record Updated');
END;
plsql
CopyEdit
BEGIN
DELETE FROM employees
WHERE empno = 1001;
DBMS_OUTPUT.PUT_LINE('Record Deleted');
END;
pl
CopyEdit
DECLARE
v_name employees.ename%TYPE;
v_salary employees.salary%TYPE;
BEGIN
SELECT ename, salary INTO v_name, v_salary
FROM employees
WHERE empno = 1001;
5. Complete Example:
plsql
CopyEdit
DECLARE
v_empno employees.empno%TYPE := 1002;
v_name employees.ename%TYPE;
BEGIN
-- Insert
INSERT INTO employees (empno, ename, salary) VALUES (v_empno, 'Alice', 6000);
-- Update
UPDATE employees SET salary = salary + 500 WHERE empno = v_empno;
-- Select Into
SELECT ename INTO v_name FROM employees WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
-- Delete
DELETE FROM employees WHERE empno = v_empno;
-- Commit
COMMIT;
END;
Cursors in PL/SQL
A Cursor in PL/SQL is a pointer to a context area that stores the result set of a query.
PL/SQL Cursors
The cursor is used to retrieve data one row at a time from the results set, unlike other SQL
commands that operate on all rows at once.
Cursors update table records in a singleton or row-by-row manner.
The Data that is stored in the Cursor is called the Active Data Set. Oracle DBMS has
another predefined area in the main memory Set, within which the cursors are opened. Hence
the size of the cursor is limited by the size of this pre-defined area.
Cursor Actions
Key actions involved in working with cursors in PL/SQL are:
1. Declare Cursor: A cursor is declared by defining the SQL statement that returns a result
set.
2. Open: A Cursor is opened and populated by executing the SQL statement defined by the
cursor.
3. Fetch: When the cursor is opened, rows can be fetched from the cursor one by one or in a
block to perform data manipulation.
4. Close: After data manipulation, close the cursor explicitly.
5. Deallocate: Finally, delete the cursor definition and release all the system resources
associated with the cursor.
Types of Cursors in PL/SQL
Cursors are classified depending on the circumstances in which they are opened.
Implicit Cursor: If the Oracle engine opened a cursor for its internal processing it is
known as an Implicit Cursor. It is created "automatically" for the user by Oracle when a
query is executed and is simpler to code.
Explicit Cursor: A Cursor can also be opened for processing data through a PL/SQL
block, on demand. Such a user-defined cursor is known as an Explicit Cursor.
Explicit cursor
An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on
a SELECT Statement which returns more than one row.
SQL%NOTFOUND Returns TRUE if DML statement did NOT affect any row.
🔹 Example:
plsql
CopyEdit
BEGIN
DELETE FROM employees WHERE deptno = 10;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows deleted.');
ELSE
DBMS_OUTPUT.PUT_LINE('No rows deleted.');
END IF;
END;
plsql
CopyEdit
CURSOR emp_cur IS SELECT * FROM employees;
Attribute Description
cursor_name%NOTFOUND Returns TRUE if the last fetch did NOT return a row.
CLOSE emp_cur;
END;
Summary Table
Attribute Implicit Cursor (SQL) Explicit Cursor (cursor_name)
%ROWCOUNT ✔ ✔
%FOUND ✔ ✔
%NOTFOUND ✔ ✔
Syntax:
For Standard FOR LOOP:
-- Basic FOR LOOP
FOR loop_index IN lower_bound..upper_bound
LOOP
-- Statements to be executed in each iteration
END LOOP;
For Reverse FOR LOOP:
-- FOR LOOP in Reverse
FOR loop_index IN REVERSE lower_bound..upper_bound
LOOP
-- Statements to be executed in each iteration
END LOOP;
Using FOR LOOP to Print Numbers
DECLARE
-- Loop index
loop_index NUMBER := 1;
BEGIN
FOR loop_index IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('Number: ' || loop_index);
END LOOP;
END;
/
Output:
Number: 1
Number: 2
Number: 3
Number: 4
5MARK
2. Describe different data types in PL/SQL and explain the use of %TYPE and %ROWTYPE.
4. What is a cursor in PL/SQL? Differentiate between implicit and explicit cursors with
examples.
5. What are exceptions in PL/SQL? Explain the types and how they are handled.
10 MARK:
1. Discuss the structure and components of a PL/SQL block. Write a program to demonstrate
variable declaration, arithmetic operations, and exception handling.
2. Explain the different types of PL/SQL cursors. Write a PL/SQL program using an explicit
cursor with parameters, and explain the use of cursor attributes.
3. Describe the various control structures available in PL/SQL. How are nested blocks and
loops implemented in a PL/SQL program? Give an example.
4. What are the different types of exceptions in PL/SQL? How are user-defined exceptions
declared and handled? Write a PL/SQL block that demonstrates both predefined and user-
defined exceptions.
5. Write a PL/SQL program that performs insert, update, and delete operations using SQL
statements embedded within PL/SQL. Use transaction control statements like COMMIT,
ROLLBACK, and SAVEPOINT to manage the changes. Explain each step clearly.
UNIT V COMPLETED