You are on page 1of 36

Chapter 1

1. Introduction to database

• A database is a structured collection of data designed for efficient storage, retrieval, and management.

• Its primary purpose is to organize data in a systematic manner, facilitating easy access and manipulation.

• Different types of databases exist, including relational (SQL), NoSQL, object-oriented, etc.

• For example: Social media platforms use databases to manage user profiles, posts, connections, and
interactions.

• Advantages of Databases:

o Databases ensure data consistency, accuracy, and reliability.

o They offer robust security measures, scalability, and concurrent access for enhanced efficiency.

2. Characteristics of Database

• Self-Describing Nature:

o The DBMS catalog stores metadata, providing descriptions of the database structure.

o This self-describing nature simplifies database management and allows for more efficient querying
and manipulation of data.

• Insulation between Programs and Data:

o Program-data independence allows modifications to data storage structures and operations without
necessitating changes in DBMS access programs.

o This insulation enables easier adaptation to evolving business needs without affecting the
applications utilizing the database.

• Data Abstraction:

o Utilizes a data model to offer users a conceptual view of the database, concealing intricate storage
details.

o Data abstraction enhances user understanding by presenting a simplified and logical view of the
database structure.

• Support of Multiple Views:


o Enables diverse users to have customized views of the database, displaying only the data relevant to
their specific needs or permissions.

o Multiple views enhance usability by tailoring data presentation based on different user roles or
requirements.

• Sharing of Data and Multi User Transaction Processing:

o Allows multiple users to concurrently access, retrieve, and update the database.

o Concurrency control mechanisms within the DBMS ensure proper execution or complete
cancellation of each transaction to maintain data consistency and integrity.

3. Properties of database

• Representation of Real-World Aspects:

o Databases aim to represent aspects of the real world. Through tables, records, and relationships,
databases emulate real-world structures to facilitate data organization and retrieval.

• Logical Coherence and Meaningful Data:

o Data within databases is logically coherent, organized in a structured manner following predefined
rules and relationships.

• Purposeful Design, Construction, and Population:

o Databases are designed, built, and populated with a specific objective in mind, addressing particular
needs or requirements.

o The creation process involves defining schemas, tables, and constraints based on the intended use
and application requirements.

• Tailored for Target Users and Applications:

o Databases are constructed considering a target group of users and specific applications they serve.

o They are optimized to efficiently store, retrieve, and manage data relevant to the intended users and
applications, ensuring performance meets their requirements.

4. File vs Database system

2
5. Users of database

• Actors on the Scene

• These individuals directly interact with, control, or use the database and its content:

o Database Administrators (DBAs):

▪ Responsible for authorizing access, coordinating and monitoring database use, acquiring
resources, controlling access, and ensuring operational efficiency.

o Database Designers:

▪ Define the database content, structure, constraints, and functions or transactions. They
liaise with end-users to understand their requirements and translate them into the database
design.

o End-users:

▪ Utilize the data for queries, reports, and some update operations.

• Categories of End-users

o Casual Users:

▪ Access the database occasionally as needed.

o Naïve or Parametric Users:

▪ Utilize pre-defined functions ("canned transactions") regularly. Examples include bank-


tellers or reservation clerks.

o Application Programmers:

▪ Develop application programs using various tools, including those for user interface
development.

o Sophisticated Users:

▪ Business analysts, scientists, engineers, etc., familiar with system capabilities, often using
software packages that work closely with the stored database.

3
o Stand-alone Users:

▪ Maintain personal databases using packaged applications, e.g., an individual using a tax
program to manage their personal database.

• Database Administrator (DBA)

o Oversee central control of data and programs accessing the data within DBMS.

o Functions include schema definition, storage structure modification, authorization management,


and routine maintenance.

• Workers Behind the Scene

• These individuals contribute to the development, implementation, and maintenance of the database system:

o DBMS System Designers and Implementers:

▪ Design and implement DBMS modules and interfaces as software packages.

o Tool Developers:

▪ Design and implement software tools facilitating database modeling, design, system
optimization, and performance enhancement.

o Operators and Maintenance Personnel:

▪ Responsible for running and maintaining the hardware and software environment for the
database system.

6. Three schema architecture

• External Schema:

o Provides a tailored view of data for each user group or application.

o Offers specific data formats & access rights unique to each user's needs

• Conceptual Schema:

o Presents a unified, logical structure of the entire database.

o Ensures consistency across different user perspectives without detailing specific user needs.

• Internal Schema:

o Describes how data is physically stored within the database.

o Focuses on optimizing storage and retrieval mechanisms.

• Operations and Mappings


4
o Request Processing:

▪ Transforms user requests from their view (external schema) to the conceptual schema.

▪ Converts these requests to operations accessing the internal schema for efficient processing.

o Mappings:

▪ Ensure accurate processing of requests between schema levels without compromising data
integrity.

o Data Formatting:

▪ Adjusts internally stored data to match the specific user's view during data retrieval.

7. Database Independence

• Data Independence refers to the database system's ability to modify one level of the system without
impacting other levels, ensuring flexibility and adaptability. There are two primary types:

• Logical Data Independence:

o Enables modifications to the conceptual schema without affecting external schemas or application
programs.

o Changes such as additions or deletions of data within the database should not disrupt or alter the
external view or access to data.

o Applications using the external schema should continue functioning normally even after significant
changes are made to the logical structure of the database.

• Physical Data Independence:

o Allows alterations to the internal schema without necessitating changes in the conceptual or
external schemas.

o Changes in the physical schema, like optimizing storage, restructuring files, or improving
performance through indexing, can be implemented without affecting the external view of data.

o Modifications to the physical organization should not demand changes in the conceptual schema as
long as the data remains consistent.

8. Database architecture

• Query Processor

5
• Handles user queries and facilitates data retrieval.

• Components:

o DDL Interpreter: Interprets and executes Data Definition Language (DDL) statements, retrieving data
definitions from the data dictionary.

o DML Compiler: Translates Data Manipulation Language (DML) statements into executable low-level
instructions for the query evaluation engine.

o Query Evaluation Engine: Executes instructions generated by the DML compiler to fetch data from
the DBMS.

• Storage Manager

• Responsible for managing data storage and ensuring data integrity.

• Components:

o Authorization and Integrity Manager: Enforces integrity constraints and manages user access rights.

o Transaction Manager: Maintains database consistency, especially during system failures or


concurrent access.

o File Manager: Controls disk storage space allocation and data structures on disk.

o Buffer Manager: Handles efficient data transfer between disk storage and main memory, optimizing
data retrieval.

• Storage Components

o Data Files: Store actual data within the database.

o Data Dictionary: Contains metadata providing essential information about the database structure.

o Indices: Facilitate swift access to specific data items, enhancing data retrieval speed.

6
9. Schema and instance

• A schema represents the logical plan defining the structure, organization, and relationships of data within a
database.

• It provides the blueprint for how data is stored, accessed, and manipulated.

• Key Points about Schemas:

o Structure Definition:

▪ Specifies the table structure, attributes (columns), data types, constraints, and relationships
between tables.

▪ Defines the layout and arrangement of data elements, ensuring data organization and
coherence.

o Data Integrity Constraints:

▪ Enforces data integrity by implementing constraints such as primary keys, foreign keys,
unique constraints, and check constraints.

▪ Ensures accuracy, consistency, and reliability of data stored within the database.

o Security and Access Control:

▪ Defines user roles and access privileges within the database environment.

▪ Specifies who has permission to view, modify, or manipulate data in tables or the entire
database.

▪ Includes security policies and access control mechanisms to safeguard sensitive information
and regulate user actions.
7
o Data Dictionary:

▪ Often contains a data dictionary that serves as a repository for metadata about the
database.

▪ Contains descriptions of tables, columns, constraints, indexes, and other database


components.

▪ Provides essential information describing the database's structure, aiding in understanding


and managing the database effectively.

Chapter 2

1. Er diagram & entity types

• ERD is a visual representation displaying relationships between entities in a database.

• Entity types are representations of real-world objects or concepts stored in the database. These entities can
be either tangible (e.g., a person, a product) or intangible (e.g., an event, an invoice).

• Entity types in an ERD encompass specific attributes defining their characteristics and properties, aiding in
the understanding of the data they store.

• ERDs employ various symbols (like rectangles for entities, lines for relationships) to depict connections,
cardinality, and the nature of associations between different entities.

• Strong Entity Sets: These entities exist independently and possess their attributes. They are represented by
rectangles in ERDs and typically have a primary key to uniquely identify each instance. Example: 'Customer'
entity in a 'Customer-Order' scenario.

• Weak Entity Sets: Weak entities depend on a related strong entity for identification and do not have a
primary key on their own. They're often depicted using double rectangles in ERDs. Example: 'OrderItem'
entity relying on 'OrderID' within the 'Order' entity, lacking its unique identification.

2. Data models

• Relational Model:

o Uses tables to represent data and relationships.

o Each table has rows (records) and columns (attributes).

o Tables follow fixed-format records with defined attributes.

• Entity-Relationship Model (E-R Model):

o Utilizes entities and relationships to represent data.

o Entities signify distinguishable real-world objects.

o Relationships illustrate connections between entities.

• Object-Based Data Model:

o Extends E-R model with encapsulation, methods, and object identity.

o Incorporates object-oriented principles within data modeling.

• Object-Relational Data Model:


8
o Combines features of object-oriented and relational models.

o Integrates object-oriented concepts with relational database capabilities.

• Semistructured Data Model:

o Allows varying attributes within the same data type.

o Differs from traditional models by permitting different attribute sets.

3. Attributes

• Simple (Atomic) Attribute:

o Attributes that cannot be divided further, such as 'Age' of a person.

• Composite Attribute:

o Attributes that can be divided into smaller subparts, representing more basic attributes with
independent meanings.

o For instance, 'Address' is composed of subparts like street number, city, state, and postal code.

• Single-Valued Attribute:

o Represents an attribute with a single value for a specific entity.

o Example: 'DateOfBirth' for an individual entity.

• Multivalued Attributes:

o Attributes holding multiple values for a particular entity.

o An example could be 'PhoneNumbers' for a person having multiple contact numbers.

• Complex Attributes:

o Attributes that combine composite and multivalued attributes, allowing nested structures.

o Consider the 'Education' attribute consisting of 'Degree' and 'University' nested within.

• Derived Attributes:

o Attribute values derived from other entities or attributes.

o An instance could be 'Age,' calculated from 'DateOfBirth' in a database.

o Another example is 'TotalCost' in an e-commerce system, derived from 'UnitPrice' and 'Quantity.'

4. Cardinality

• Cardinality describes the number of entities associated with another entity through a relationship set in a
database.

• Cardinality is primarily used to comprehend the relationships between two entities within a database.

• Types of Mapping Cardinality in Binary Relationships:

o One-to-One (1:1):

▪ Each entity in one set is linked to precisely one entity in another set.

▪ Example: A marriage relationship between two individuals, where each person is married to
only one other person.

9
o One-to-Many (1:N):

▪ Each entity in one set can be associated with multiple entities in another set, but each entity
in the second set is connected to only one entity in the first set.

▪ Example: A university department (one) having multiple students (many), yet each student is
affiliated with only one department.

o Many-to-One (N:1):

▪ Multiple entities in one set can relate to only one entity in another set.

▪ Example: Many employees (many) belong to a single department (one), but each
department can have multiple employees.

o Many-to-Many (N:M):

▪ Multiple entities in one set can be associated with multiple entities in another set.

▪ Example: Students (many) enrolling in multiple courses (many), and courses having multiple
students enrolled concurrently.

5. Participation

• Participation describes the involvement of entities from an entity set in relationships within a relationship
set in a database schema.

• Total Participation (Indicated by Double Line):

o Total participation involves every entity within an entity set participating in at least one relationship
within the relationship set.

o Contributes to data integrity and completeness.


10
o Example: Consider the relationship between 'student' and 'advisor.' If the participation of students in
the advisor relationship is total, it means every student must have an associated advisor. No student
can exist without an advisor relationship.

• Partial Participation:

o In contrast, partial participation refers to a scenario where some entities within an entity set may
not participate in any relationship within the relationship set.

o Offers flexibility, permitting some entities in the entity set to remain unconnected in certain
relationships

o Example: In the relationship between 'instructor' and 'advisor,' if the participation of instructors in
the advisor relationship is partial, it implies that some instructors may not be associated with any
advisor relationship. They might not have an advisor assigned to them.

6. Extended Entity-Relationship Model: Generalization,Specialization & Aggregation

• The Extended Entity-Relationship model is an advanced and flexible database design concept that enhances
the Entity-Relationship model.

• Generalization:

o It's a bottom-up approach merging lower-level entities that share common attributes to create a
higher-level entity.

o Example: In a university database, "Person" can be a general entity from which "Student" and
"Faculty" inherit common attributes like "Name" and "Address."

• Specialization:

o A top-down approach that divides a higher-level entity into multiple lower-level entities based on
specific attributes or relationships.

o Example: In a vehicle database, "Vehicle" can be specialized into "Car" and "Motorcycle," each with
unique attributes like "Number of Doors" for cars and "Engine Displacement" for motorcycles.

• Aggregation:

o Represents whole-part relationships, combining smaller related entities to form a higher-level entity.

o Example: In an e-commerce system, an "Order" entity can aggregate multiple "Order Items," each
representing a product, quantity, and price.

o These concepts within the Extended Entity-Relationship model enhance the traditional Entity-
Relationship model by allowing for more complex and structured data representation.

11
7. Er to Schema

8. Keys

• Key is a specific attribute or combination of attributes within an entity or relation (table) that uniquely
identifies each tuple (row) in the entity.

• They ensure data uniqueness,relational connections,efficient data retrieval

• Primary Key:

o A primary key is a unique attribute or a combination of attributes within a table that uniquely
identifies each row in that table.

o Purpose: Ensures data integrity and acts as the main link between different tables in a relational
database.

o Example: In an "Employees" table, the "EmployeeID" attribute serves as the primary key.

• Candidate Key:

o Definition: A candidate key is an attribute or a set of attributes that could potentially function as a
primary key due to their unique identification property.

o Purpose: Provides multiple options for choosing the primary key.

o Example: In the same "Employees" table, both "EmployeeID" and "Social Security Number" could be
candidate keys.

• Superkey:

o Definition: A superkey is a set of attributes that uniquely identifies tuples but may contain more
attributes than required.
12
o Purpose: Represents a broader identification set that includes attributes not strictly necessary for
uniqueness.

o Example: A superkey for the "Employees" table might include "EmployeeID," "First Name," and "Last
Name."

• Foreign Key:

o Definition: A foreign key is an attribute in one table that references the primary key in another table,
establishing a relationship between the two tables.

o Purpose: Creates relationships between tables to enforce referential integrity.

o Example: In an "Orders" table, a "CustomerID" attribute might serve as a foreign key, linking each
order to a specific customer in the "Customers" table.

9. Relational model

• Relational Model:

• The relational model is a database management approach that organizes data into tables (relations)
consisting of rows (tuples) and columns (attributes).

• Developed by E.F. Codd in the 1970s, it forms the foundation for most modern database systems.

• Key Characteristics:

o Data is structured into tables representing entities and relationships.

o Each table contains rows representing individual records and columns representing specific
attributes or properties.

o Utilizes the principles of set theory and predicate logic to manipulate data.

10. Relational schema

• A relational schema is a blueprint that defines the structure, constraints, and relationships of tables in a
relational database.

• It outlines the logical view of the database and describes how data is organized and stored.

• Components of Relational Schema:

o Table Definition: Specifies table names and their associated attributes.

o Constraints: Enforces rules and restrictions on data integrity, such as primary keys, foreign keys, and
unique constraints.

o Relationships: Defines associations between tables, establishing connections using keys.

11. ORDBMS vs RDBMS

1. RDBMS :
RDBMS stands for Relational Database Management System.

In this database management, the data is organized into the related tables. To access the database it uses Structured
Query Language (SQL). This model is based on the mathematical theory of relational algebra and calculus. The
original concept for the model is proposed by Dr. E.F. Codd in a 1970. After some time the model was classified by

13
defining twelve rules which are known as Codd’s rule. For any database to be relational database it must satisfy
atleast 6 out of 12 Codd’s rules. These 12 Codd’s rule are as follows :

• Information Rule

• Guaranteed Access Rule

• Systematic Treatment Of Null Values

• Database Description Rule

• Comprehensive Data Sub-Language Rule

• View Updating Rule

• High Level Insert, Update and Delete

• Physical Data Independence

• Logical Data Independence

• Integrity Independence

• Distribution Independence

• Non Subversion Rule

2. ORDBMS :
ORDBMS stands for Object-Relational Database Management System.

It provides all the facilities of RDBMS with the additional support of object oriented concepts. The concept of classes,
objects and inheritance are supported in this database. It is present in the ground level between the RDBMS and
OODBMS. In this data can be manipulated by using any query language. It is complex because it has to take care of
both Relational database concepts and as well as Object Oriented concepts. Some of the object related DBMS
available in the market are as follows :

• IBM’S DB2 Universal Database system

• Informix’s Universal server

Difference between RDBMS and ORDBMS :

S.No. RDBMS ORDBMS

RDBMS is a Relational Database ORDBMS is a Object Oriented Relational Database


1. Management System based on the Management System based on the Relational as well as
Relational model of data. Object Oriented database model.

It follows table structure, it is simple to It is same as RDBMS but it has some extra confusing
2.
use and easy to understand. extensions because of the Object Oriented concepts.

3. It has no extensibility and content. It is only limited to the new data-types.

14
S.No. RDBMS ORDBMS

4. Since RDBMS is old so, it is very mature. It is developing so it is immature in nature.

In this, there is extensive supply of tools


5. It can take the advances of RDBMS tools and developers.
and trained developers.

It has poor support for Object-Oriented


6. It supports the features of object-oriented programming.
programming.

It supports Structured Query Language


7. It supports Object Query Language (OQL).
(SQL).

RDMS is used for traditional applications


8. tasks such as data administration and ORDMS is used for applications with complex objects.
data processing.

9. It is capable of handling only simple data. It is also capable of handling the complex data.

MS SQL server, MySQL, SQLite, MariaDB


10 PostgreSQL is an example of ORDBMS.
are examples of RDBMS.

12. Relational algebra

• The algebraic operations create new relations by manipulating existing ones using operations within the
algebra.

• Operations can be chained or combined to form relational algebra expressions, defining complex queries or
data manipulations.

• The output of a relational algebra expression is a relation, representing the result of a database query or
retrieval request.

• Relational algebra forms the fundamental operations for the relational model, allowing users to specify basic
retrieval requests or queries.

• All operations in relational algebra produce relations as output, making the algebra "closed" as all objects
within it are relations.

• Types of Relational Algebra Operations:

• Unary Relational Operations:

o SELECT (σ):

▪ Filters rows from a relation based on a specified condition.


15
o PROJECT (π):

▪ Selects specific columns from a relation, removing duplicates.

o RENAME (ρ):

▪ Changes the name of a relation or its attributes.

• Operations from Set Theory:

o UNION (∪), INTERSECTION (∩), DIFFERENCE (or MINUS, –):

▪ Set-based operations to combine or compare relations.

o CARTESIAN PRODUCT (×):

▪ Combines tuples from two relations to create a new relation.

13. Select

• Purpose: Filters rows from a relation based on specified conditions.

• Symbol: Represented by σ (sigma).

• Functionality: Retrieves rows that satisfy specified criteria.

• Syntax: σ<condition>(Relation)

• Example: σ(Salary > 50000)(Employees)

• Result: Generates a new relation with rows meeting the condition.

14. Project

• Purpose: Selects specific columns from a relation.

• Symbol: Represented by π (pi).

• Functionality: Retrieves chosen attributes from a relation, eliminating duplicates.

• Syntax: π<attribute(s)>(Relation)

• Example: π(Name, Salary)(Employees)

• Result: Generates a new relation with specified attributes.

15. Rename

• Purpose: Changes the name of a relation or its attributes.

• Symbol: Represented by ρ (rho).

• Functionality: Renames the relation or its attributes for clarity or convenience.

• Syntax: ρ<NewName/Attribute>(OldName)

• Example: ρ(NewSalary)(Salary)

• Result: Renames the attribute "Salary" to "NewSalary" in the relation.


16
16. UNION (∪)

• Purpose: Combines tuples from two relations, removing duplicates.

• Symbol: Represented by ∪ (union).

• Functionality: Merges rows from two relations into a single relation.

• Syntax: R ∪ S (R and S are relations with the same attributes)

• Example: If R represents the set of employees in Department A and S represents employees in Department
B, R ∪ S would provide a combined list of employees from both departments, removing duplicates.

• Result: Generates a new relation with all distinct tuples from both relations.

17. INTERSECTION (∩)

• Purpose: Retrieves common tuples from two relations.

• Symbol: Represented by ∩ (intersection).

• Functionality: Selects rows that appear in both relations.

• Syntax: R ∩ S (R and S are relations with the same attributes)

• Example: If R represents the set of employees in Department A and S represents employees in Department
B, R ∩ S would provide a list of employees who belong to both departments.

• Result: Generates a new relation with tuples that exist in both input relations.

18. SET DIFFERENCE (or MINUS, −)

• Purpose: Retrieves tuples present in one relation but not in another.

• Symbol: Represented by MINUS (−) or the subtraction symbol.

• Functionality: Subtracts tuples from one relation that also exist in another.

• Syntax: R - S (R and S are relations with the same attributes)

• Example: If R represents the set of all employees and S represents employees in Department A, R - S would
provide a list of employees not belonging to Department A.

• Result: Generates a new relation with tuples from the first relation that are not present in the second
relation.

19. JOIN

• Purpose: Combines tuples from two relations based on a specified condition.

• Functionality: Merges rows from two relations that satisfy a given condition.

• Types: Various types of joins exist—INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, etc.

• Syntax (for INNER JOIN): R ⨝<condition> S (R and S are relations)

17
• Example: If R represents the set of employees and S represents their respective departments, R ⨝
(R.DepartmentID = S.DepartmentID) S would combine employee data with their corresponding departments.

• Result: Generates a new relation by matching tuples from both input relations based on the specified
condition.

20. EQUIJOIN

• Purpose: Performs a type of JOIN operation based on equality between attributes.

• Functionality: Merges rows from two relations where specified attributes are equal.

• Syntax: R ⨝ (R.Attribute = S.Attribute) S (R and S are relations)

• Example: If R represents the set of employees and S represents their respective departments, R ⨝
(R.DepartmentID = S.DepartmentID) S would combine employee data with their corresponding departments
where the DepartmentID matches.

• Result: Generates a new relation by matching tuples from both input relations where the specified attributes
are equal.

21. NATURAL JOIN

• Purpose: Performs a JOIN operation based on common attributes between two relations.

• Functionality: Merges rows from two relations with matching attribute names.

• Syntax: R ⨝ NATURAL JOIN S (R and S are relations)

• Example: If R represents employee data and S represents departments, R ⨝ NATURAL JOIN S would
combine data where both relations share common attribute names, such as "DepartmentID".

• Result: Generates a new relation by combining tuples from both input relations based on their shared
attribute names.

22. Aggregate functions

• Purpose: Computes summaries or aggregations of data within a relation.

• Usage: Operates on groups of tuples to generate a single result.

• Functions: Common aggregate functions include:

o SUM: Computes the sum of values in a specified attribute.

o COUNT: Calculates the number of tuples or non-null values.

o AVG: Determines the average of values in a specified attribute.

o MIN: Finds the minimum value in a specified attribute.

o MAX: Identifies the maximum value in a specified attribute.

• Syntax: AggregateFunction<Attribute>(Relation)

• Example: COUNT(EmployeeID)(Employees) - Counts the number of EmployeeID entries in the Employees


relation.

18
• Result: Returns a single value that represents the result of the aggregate function applied to the specified
attribute in the relation.

23. OUTER JOIN Operation

• Purpose: Performs a JOIN operation, including unmatched tuples from one or both relations.

• Types: Includes LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.

• Functionality: Combines tuples from two relations based on a specified condition and includes unmatched
tuples.

• Syntax (for LEFT OUTER JOIN): R ⟕<condition> S (R and S are relations)

• Example: If R represents employees and S represents departments, R ⟕ (R.DepartmentID = S.DepartmentID)


S would combine employee data and include departments even if no employee belongs to that department.

• Result: Generates a new relation by matching tuples based on the condition and including unmatched tuples
from one or both relations.

Chapter 3

1. Overview of SQL

• Data-definition language(DDL):

o The SQL DDL provides commands for defining relation schemas, deleting relations, and modifying
relation schemas.

• Data-manipulation language(DML):

o The SQL DML provides the ability to query information from the database and to insert tuples into,
delete tuples from, and modify tuples in the database.

• Integrity:

o The SQL DDL includes commands for specifying integrity constraints that the data stored in the
database must satisfy. Updates that violate integrity constraints are disallowed.

• View definition:

o The SQL DDL includes commands for defining views.

• Transaction control:

o SQL includes commands for specifying the beginning and ending of transactions.

• Embedded and dynamic SQL:

o Define how SQL statements can be embedded within general-purpose programming languages, such
as C, C++, and Java.

• Authorization:

o The SQL DDL includes commands for specifying access rights to relations and views.

2. Domains of SQL

• char(n): Fixed length, user-defined size string.

• varchar(n): Variable length, user-defined maximum size string.


19
• int: Machine-dependent integer.

• smallint: Machine-dependent small integer.

• numeric(p,d): Fixed point number with precision p and d digits to the right of the decimal point.

• real, double precision: Floating-point numbers with machine-dependent precision.

• float(n): Floating-point number with at least n digits of precision.

3. DDL commands

• CREATE Command:

o Used to create new database objects like tables, views, indexes, etc.

o Syntax for creating a table: CREATE TABLE table_name (column1 datatype, column2 datatype, ...);

o Requires specifying table name, column names, & their data types.

o Example: CREATE TABLE students (id INT, name VARCHAR(50), age INT);

• DROP Command:

o Used to remove database objects like tables, views, indexes, etc.

o Syntax for dropping a table: DROP TABLE table_name;

o Irreversibly deletes the entire table and its data.

o Example: DROP TABLE employees;

• ALTER Command:

o Modifies the structure of existing database objects like tables.

o Syntax for adding a column: ALTER TABLE table_name ADD column_name datatype;

o Allows adding, modifying, or dropping columns, changing data types, etc.

o Example: ALTER TABLE customers ADD email VARCHAR(100);

• TRUNCATE Command:

o Removes all records from a table but keeps the table structure intact.

o Syntax: TRUNCATE TABLE table_name;

o Faster than DELETE as it doesn't generate individual deletion logs for each row.

o Example: TRUNCATE TABLE logs;

• RENAME Command:

o Renames an existing database object.

o Syntax for renaming a table: RENAME TABLE old_table_name TO new_table_name;

o Useful for altering object names without changing their structures.

o Example: RENAME TABLE users TO customers;

4. DML commands

• SELECT Command:

20
o Fundamental command used to retrieve data from a database.

o Syntax: SELECT column1, column2 FROM table_name WHERE condition;

o Allows fetching specific columns or all columns from a table.

o Distinct keyword is used for duplicate removal and * is used for selecting all records

o The WHERE clause is optional but used for filtering data based on specified conditions.

o Example: SELECT * FROM customers WHERE country='USA';

• INSERT Command:

o Used to add new records/rows into a database table.

o Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

o Requires specifying the table and columns where data needs to be inserted along with
corresponding values.

o Example: INSERT INTO employees (emp_name, emp_salary) VALUES ('John Doe', 50000);

• UPDATE Command:

o Modifies existing records in a table.

o Syntax: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

o Indicates which columns to update and their new values.

o The WHERE clause is crucial to specify which records to update; without it, all records in the table
might be affected.

o Example: UPDATE products SET quantity = 100 WHERE product_id = 123;

• DELETE Command:

o Removes records from a table.

o Syntax: DELETE FROM table_name WHERE condition;

o Requires the WHERE clause to prevent accidentally deleting all records and to specify which records
to remove based on certain conditions.

o Example: DELETE FROM customers WHERE customer_id = 456;

• Where:

o The where clause specifies conditions that the result must satisfy

o SQL includes a between comparison operator to find records by specifying range

o select loan_number from loan where branch_name = ‘ Perryridge’ and amount > 1200

• From:

o The from clause lists the relations involved in the query

o You can involve multiple relation in query by using comma as separator

o select * from borrower, loan

5. Constraints

21
• Key Constraints:

o Primary Key Constraint: Ensures uniqueness and non-null values in a column or a set of columns.

o It uniquely identifies each record in a table.

o Example: CREATE TABLE students (student_id INT PRIMARY KEY, name VARCHAR(50));

o Unique Constraint: Restricts values in a column(s) to be unique but allows NULL values.

o Example: CREATE TABLE employees (emp_id INT UNIQUE, email VARCHAR(100));

• Domain Constraints:

o Defines allowable data values for a column.

o Specifies the data type for a column.

o Example: CREATE TABLE products (product_id INT, product_name VARCHAR(50));

• Referential Integrity:

o Maintains relationships between tables using foreign keys.

o FOREIGN KEY: Ensures data consistency by enforcing a link between two tables.

o Example: CREATE TABLE orders (order_id INT, product_id INT, FOREIGN KEY (product_id)
REFERENCES products(product_id));

• Check Constraints:

o Imposes conditions on data entered into a table.

o Verifies that data meets a specific condition.

o Example: CREATE TABLE employees (emp_id INT, age INT CHECK (age >= 18));

• Not null:

o The not null constraint on an attribute specifies that the null value is not allowed for that attribute

o CREATE TABLE ConstraintDemo1(ID INT NOT NULL, Name VARCHAR(50) NULL)

6. DCL commands

• GRANT Command:

o Allows users to grant specific privileges or permissions to database objects (such as tables, views,
procedures) to other users or roles.

o Syntax: GRANT privilege_name ON object_name TO {user_name | PUBLIC | role_name};

o privilege_name can be SELECT, INSERT, UPDATE, DELETE, ALL, etc.

o object_name refers to the database object on which the privileges are granted.

o user_name, PUBLIC, or role_name specifies the users, public, or roles to whom the privileges are
granted.
22
o Example: GRANT SELECT ON employees TO manager;

• REVOKE Command:

o Allows users to revoke previously granted privileges or permissions from other users or roles.

o Syntax: REVOKE privilege_name ON object_name FROM {user_name | PUBLIC | role_name};

o Similar parameters to the GRANT command for specifying privilege, object, and user/role.

o Example: REVOKE INSERT ON customers FROM salesperson;

7. TCl commands

• COMMIT Command:

o Definition: COMMIT is a vital TCL command used in SQL to permanently save changes made within a
transaction.

o Syntax: COMMIT;

o Example:

▪ BEGIN TRANSACTION;

▪ -- Execute SQL statements (INSERT, UPDATE, DELETE)

▪ COMMIT;

o Explanation: The COMMIT command finalizes and makes permanent all the changes made during
the transaction. For instance, after inserting records into a database table within a transaction,
executing COMMIT; will permanently save those records, making them visible to other transactions.

• ROLLBACK Command:

o Definition: ROLLBACK is a crucial TCL command used to undo changes within a transaction that has
not been committed.

o Syntax: ROLLBACK;

o Example:

▪ BEGIN TRANSACTION;

▪ -- Execute SQL statements (INSERT, UPDATE, DELETE)

▪ ROLLBACK;

o Explanation: Upon executing ROLLBACK, any modifications made within the transaction are
discarded, reverting the database to its state before the transaction began. For instance, if an error
occurs during an operation within a transaction, executing ROLLBACK; will undo those changes,
ensuring database consistency.

• SAVEPOINT Command:

o Definition: SAVEPOINT sets a named point within a transaction to which you can later roll back.

o Syntax: SAVEPOINT savepoint_name;

o Example:

▪ BEGIN TRANSACTION;

23
▪ -- Execute SQL statements

▪ SAVEPOINT sp1;

▪ -- Further SQL operations

▪ ROLLBACK TO SAVEPOINT sp1;

o Explanation: SAVEPOINT divides a transaction into smaller parts and allows rolling back to a specific
point within the transaction, enhancing flexibility. If a part of the transaction encounters an issue,
using ROLLBACK TO SAVEPOINT allows reverting to a designated SAVEPOINT, ensuring data
consistency.

8. String operations

• Concatenation:

o Concatenation is the process of combining two or more strings into a single string.

o Syntax: In SQL, concatenation can be achieved using the CONCAT() function or the + operator.

o Example:

o SELECT CONCAT('Hello', ' ', 'World') AS ConcatenatedString;

o -- Output: 'Hello World'

o Explanation: Concatenation allows combining strings together, useful for creating full names,
addresses, or custom messages within SQL queries by joining multiple string values.

• UPPER() & LOWER():

o UPPER() and LOWER() are SQL functions used to convert strings to uppercase and lowercase,
respectively.

o Syntax:

o UPPER(string) converts a string to uppercase.

o LOWER(string) converts a string to lowercase.

o Example:

o SELECT UPPER('hello') AS UppercaseString, LOWER('WORLD') AS LowercaseString;

o -- Output: 'HELLO' and 'world'

o Explanation: These functions are helpful for standardizing text input, making searches case-
insensitive, or displaying consistent text formatting.

• SUBSTRING:

o Definition: SUBSTRING retrieves a portion of a string based on specified starting position and length.

o Syntax:

o SUBSTRING(string, start_position, length) extracts a substring from the given string.

o Example:

o SELECT SUBSTRING('Hello World', 7, 5) AS SubstringResult;

o -- Output: 'World'

24
o Explanation: SUBSTRING extracts part of a string, starting from a specified position and of a specified
length. This is useful for extracting specific segments of text from larger strings.

• Order by:

o Used to list records in alphabetic order

o Syntax: order by column_name keyword

o Keyword are ‘asc’ and ‘desc’ resp ascending and descending

o Example: order by first_name asc

9. Set operation

• UNION:

o UNION is a set operation in SQL that combines the results of two or more SELECT statements into a
single result set.

o Syntax:

SELECT column1, column2 FROM table1

UNION

SELECT column1, column2 FROM table2;

o Example:

SELECT employee_id, employee_name FROM employees

UNION

SELECT contractor_id, contractor_name FROM contractors;

o Explanation: UNION merges the results of multiple SELECT queries, eliminating duplicates, and returns a
combined result set containing unique records from both queries.

• INTERSECT:

o INTERSECT is a set operation that returns common records present in the results of two SELECT
statements.

o Syntax:

o SELECT column1, column2 FROM table1

o INTERSECT

o SELECT column1, column2 FROM table2;

o Example:

o SELECT product_id, product_name FROM online_store

o INTERSECT

o SELECT product_id, product_name FROM physical_store;

o Explanation: INTERSECT retrieves the records that are common or shared between the result sets of
both SELECT queries, returning only the matching rows.

• EXCEPT:
25
o EXCEPT is a set operation that returns records present in the first SELECT statement but not in the
second SELECT statement.

o Syntax:

o SELECT column1, column2 FROM table1

o EXCEPT

o SELECT column1, column2 FROM table2;

o Example:

o SELECT customer_id, customer_name FROM online_subscribers

o EXCEPT

o SELECT customer_id, customer_name FROM physical_store_customers;

o Explanation: EXCEPT retrieves records from the first query that are not present in the result set of
the second query, effectively performing a set difference operation.

10. Aggregate functions

• Aggregate Functions:

o Aggregate functions perform calculations on a set of values and return a single result. Common
aggregate functions include AVG, MIN, MAX, SUM, and COUNT.

o Examples:

▪ AVG(column_name) calculates the average value of a numeric column.

▪ MIN(column_name) retrieves the minimum value from a column.

▪ MAX(column_name) fetches the maximum value from a column.

▪ SUM(column_name) computes the total sum of values in a column.

▪ COUNT(column_name) counts the number of values in a column.

o Usage:

o SELECT AVG(salary) AS average_salary FROM employees;

o SELECT MIN(age) AS youngest_employee_age FROM employees;

o SELECT MAX(age) AS oldest_employee_age FROM employees;

o SELECT SUM(sales) AS total_sales FROM transactions;

o SELECT COUNT(*) AS total_records FROM records;

• Group By:

o Definition: GROUP BY is used with aggregate functions to group rows that have the same values into
summary rows. It divides the result set into groups based on a specified column.

o Syntax:

o SELECT column_name, aggregate_function(column_name)

o FROM table_name
26
o GROUP BY column_name;

o Example:

o SELECT department, AVG(salary) AS avg_salary_per_department

o FROM employees

o GROUP BY department;

o Explanation: GROUP BY combines rows with the same values in the specified column(s) and
performs aggregate functions within each group, producing summary results for each distinct group.

• Having Clause:

o Definition: HAVING is used in combination with GROUP BY to filter the groups based on specified
conditions applied to the result of aggregate functions.

o Syntax:

o SELECT column_name, aggregate_function(column_name)

o FROM table_name

o GROUP BY column_name

o HAVING condition;

o Example:

o SELECT department, AVG(salary) AS avg_salary_per_department

o FROM employees

o GROUP BY department

o HAVING AVG(salary) > 50000;

o Explanation: HAVING clause filters groups based on the result of aggregate functions. In this
example, it selects departments with an average salary greater than $50,000.

11. Views

• View:

o A view in SQL is a virtual table based on the result set of a SELECT query. It represents a stored SQL
query that acts as a table, allowing users to retrieve specific data without duplicating the query logic.

o Creation Syntax:

o CREATE VIEW view_name AS

o SELECT column1, column2...

o FROM table_name

o WHERE condition;

o Example:

o CREATE VIEW sales_summary AS

o SELECT product_name, SUM(sales_amount) AS total_sales

27
o FROM sales

o GROUP BY product_name;

o Explanation: The view "sales_summary" summarizes sales data by product, allowing users to query it
as if it were a table, simplifying complex queries by encapsulating logic.

• Updation of View:

o Definition: Views in SQL are generally read-only, meaning they cannot be directly updated using
INSERT, UPDATE, or DELETE statements unless they meet specific criteria.

o Modification of Underlying Tables: To update a view indirectly, the underlying tables on which the
view is based must be modified using appropriate SQL operations. Changes in the base tables are
reflected in the view.

• Materialized View:

o A materialized view is a physical copy or snapshot of the data from the base tables at a specific point
in time. Unlike a regular view, a materialized view stores the results of the query and updates
periodically or on demand.

o Creation Syntax:

o CREATE MATERIALIZED VIEW mv_name AS

o SELECT column1, column2...

o FROM table_name

o WHERE condition;

o Example:

o CREATE MATERIALIZED VIEW daily_sales_summary AS

o SELECT date, SUM(sales_amount) AS total_sales

o FROM sales

o GROUP BY date;

o Explanation: A materialized view precomputes and stores the aggregated data, enhancing query
performance by reducing the need for complex calculations in real-time

12. Joins

• Joins in SQL are used to combine rows from different tables based on a related column between them. There
are different types of joins:

• INNER JOIN:

o INNER JOIN returns rows from both tables that have matching values in the specified columns.

o Syntax:

o SELECT table1.column_name, table2.column_name

o FROM table1

o INNER JOIN table2 ON table1.column_name = table2.column_name;

o Example:
28
o SELECT employees.name, departments.department_name

o FROM employees

o INNER JOIN departments ON employees.department_id = departments.department_id;

o Explanation: INNER JOIN retrieves rows from both "employees" and "departments" tables where the
"department_id" column matches, combining related information from both tables.

• LEFT JOIN (or LEFT OUTER JOIN):

o LEFT JOIN returns all rows from the left table and matching rows from the right table. If there are no
matches, NULL values are returned for the right table.

o Syntax:

o SELECT table1.column_name, table2.column_name

o FROM table1

o LEFT JOIN table2 ON table1.column_name = table2.column_name;

o Example:

o SELECT employees.name, departments.department_name

o FROM employees

o LEFT JOIN departments ON employees.department_id = departments.department_id;

o Explanation: LEFT JOIN retrieves all rows from "employees" table and matching rows from
"departments" table, providing NULL for department information if no match exists.

• RIGHT JOIN (or RIGHT OUTER JOIN):

o RIGHT JOIN returns all rows from the right table and matching rows from the left table. If there are
no matches, NULL values are returned for the left table.

o Syntax:

o SELECT table1.column_name, table2.column_name

o FROM table1

o RIGHT JOIN table2 ON table1.column_name = table2.column_name;

o Example:

o SELECT employees.name, departments.department_name

o FROM employees

o RIGHT JOIN departments ON employees.department_id = departments.department_id;

o Explanation: RIGHT JOIN retrieves all rows from "departments" table and matching rows from
"employees" table, providing NULL for employee information if no match exists.

• FULL JOIN (or FULL OUTER JOIN):

o FULL JOIN returns all rows when there is a match in either the left or right table. If no match exists,
NULL values are returned for the columns.

o Syntax:

29
o SELECT table1.column_name, table2.column_name

o FROM table1

o FULL JOIN table2 ON table1.column_name = table2.column_name;

o Example:

o SELECT employees.name, departments.department_name

o FROM employees

o FULL JOIN departments ON employees.department_id = departments.department_id;

o Explanation: FULL JOIN retrieves all rows from both tables, combining related information where
matches exist, and providing NULL where there is no match.

• NATURAL JOIN:

o A Natural Join is a type of join that performs the join operation based on columns with the same
name in both tables. It automatically matches columns with identical names and retrieves the
results.

o Syntax:

o SELECT table1.column1, table1.column2, table2.column1, table2.column2...

o FROM table1

o NATURAL JOIN table2;

o Example:

o SELECT employees.name, employees.department_id, departments.department_name

o FROM employees

o NATURAL JOIN departments;

o Explanation: NATURAL JOIN automatically matches columns with identical names (e.g.,
"department_id") between the "employees" and "departments" tables, combining the data based
on these common columns.

13. Triggers

• Triggers in SQL are database objects that automatically perform actions (such as inserting, updating,
deleting, etc.) in response to specific events occurring on tables. These events could include INSERT,
UPDATE, DELETE operations on the table.

• Types of Triggers:

o BEFORE Trigger: Executes before the triggering event (e.g., BEFORE INSERT).

o AFTER Trigger: Executes after the triggering event (e.g., AFTER UPDATE).

• Syntax:

• CREATE TRIGGER trigger_name

• {BEFORE/AFTER} {INSERT/UPDATE/DELETE} ON table_name

• FOR EACH ROW

30
• BEGIN

• -- SQL statements

• END;

• Example:

• CREATE TRIGGER update_salary_trigger

• AFTER UPDATE ON employees

• FOR EACH ROW

• BEGIN

• INSERT INTO salary_changes (employee_id, old_salary, new_salary, change_date)

• VALUES (OLD.employee_id, OLD.salary, NEW.salary, NOW());

• END;

• Explanation: This trigger, named "update_salary_trigger," fires after an update operation on the
"employees" table. It logs salary changes in the "salary_changes" table, recording the employee ID, old
salary, new salary, and the change date.

• OLD and NEW Pseudo Records: In triggers, the OLD pseudo record refers to the old values of the row being
updated or deleted, while the NEW pseudo record represents the new values during updates or inserts.

• Syntax of Dropping Triggers:

• DROP TRIGGER IF EXISTS trigger_name;

• Example:

• DROP TRIGGER IF EXISTS update_salary_trigger;

• Explanation: This command drops the trigger named "update_salary_trigger" if it exists in the database.

14. Security and authorization

• Security and authorization in SQL databases are essential to control access, protect sensitive data, and
ensure data integrity. Here are the fundamental aspects:

• User Authentication:

o User Accounts: SQL databases manage user accounts with usernames and passwords to authenticate
users.

o Authentication Methods: Common methods include password-based authentication, integrated


Windows authentication, and certificate-based authentication.

• Authorization:

o GRANT Statement: Allows users or roles specific permissions to perform operations on database
objects.

o REVOKE Statement: Withdraws previously granted permissions from users or roles.

o Example:

o GRANT SELECT, INSERT ON table_name TO user_name;

31
o REVOKE UPDATE ON table_name FROM user_name;

• Types of Permissions:

o SELECT: Grants permission to retrieve data from tables.

o INSERT: Allows inserting new records into tables.

o UPDATE: Enables modifying existing records in tables.

o DELETE: Permits the removal of records from tables.

o ALL PRIVILEGES: Grants all possible permissions on a specific database object.

• Database Roles:

o Roles are named groups of related privileges assigned to users. They simplify permissions
management by assigning privileges to roles rather than individual users.

o Example:

o CREATE ROLE admin_role;

o GRANT ALL PRIVILEGES ON database_name.* TO admin_role;

• Views for Security:

o Restricted Access: Views can restrict access to sensitive data by displaying only specific columns or
rows to certain users or roles.

o Example:

o CREATE VIEW sensitive_view AS

o SELECT sensitive_column

o FROM sensitive_table

o WHERE condition;

Chapter 4

1. Pitfalls in Relational-Database designs

• Inadequate Indexing Strategies:

o Pitfall: Failure to establish proper indexes or creating excessive indexes can impact query
performance. Over-indexing can lead to slower data modification operations, while insufficient
indexing may hinder query performance.

o Solution: Identify key columns for indexing based on query patterns and usage. Avoid unnecessary
indexing and periodically review and optimize indexes for optimal database performance.

• Normalization Oversight:

32
o Pitfall: Ignoring normalization principles may result in data redundancy, anomalies, and difficulties in
maintaining consistency.

o Solution: Adhere to normalization rules (1NF, 2NF, 3NF) to structure data efficiently, minimize
redundancy, and ensure data integrity.

• Data Integrity Constraint Neglect:

o Pitfall: Neglecting to enforce data integrity constraints (foreign keys, unique constraints) can lead to
data inconsistencies or invalid entries.

o Solution: Implement constraints to maintain data integrity, ensuring that data aligns with defined
rules and relationships.

• Inefficient Query Design:

o Pitfall: Poorly crafted queries, inefficient join types, or lack of query optimization may lead to
performance degradation.

o Solution: Optimize queries by analyzing execution plans, employing suitable indexing, and avoiding
unnecessary operations to enhance performance.

2. Concept of normalization

• Normalization is the process of organizing data in a database to reduce redundancy and dependency.

• It aims to create well-structured relations (tables) by breaking them down into smaller, more manageable
entities.

• Normalization Forms:

o 1NF (First Normal Form)

o 2NF (Second Normal Form)

o 3NF (Third Normal Form)

o BCNF(Boyce Codd Normal Form)

• Purpose and Benefits:

o Data Integrity: Normalization reduces data redundancy, minimizing anomalies and inconsistencies in
the database.

o Optimized Storage: It optimizes storage space by organizing data efficiently and eliminates
duplication.

• Process of Normalization:

o Identify Entities: Recognize entities and their attributes within the database.

o Define Relationships: Establish relationships between entities using primary and foreign keys.

o Apply Normalization Rules: Organize data to adhere to normalization forms (1NF, 2NF, 3NF) by
eliminating redundancy and dependencies.

• Example:

o Scenario: Consider a table containing customer information (CustomerID, Name, Phone, Email) and
orders (OrderID, CustomerID, Date, Product).

33
o Normalization Steps: Break the table into separate entities (Customers and Orders), establish
relationships, and apply normalization rules to ensure each table satisfies the required normal
forms.

3. Function Dependencies

• Function dependencies are a fundamental concept in database design that describe the relationship
between attributes within a relation.

• Functional Dependency (FD):

o A functional dependency occurs when the value of one attribute (or a set of attributes) in a relation
uniquely determines the value of another attribute.

o It is denoted as X -> Y, where X determines Y. If two rows have the same value for X, they must also
have the same value for Y.

• Types of Functional Dependencies:

o Full Functional Dependency:

▪ When an attribute is functionally dependent on a whole composite key (multiple attributes


forming the primary key), it's a full functional dependency.

o Transitive Dependency:

▪ Occurs when an attribute is functionally dependent on another non-key attribute instead of


a primary key.

• Understanding function dependencies aids in normalizing a database by identifying and eliminating


redundancy.

• Determining Functional Dependencies:

o Observing Data Patterns: Analyze the data to identify patterns where one attribute uniquely
determines another.

o Understanding Business Rules: Consider business rules and relationships between attributes to
establish functional dependencies accurately.

• Example:

34
o Scenario: Consider a table with attributes (StudentID, CourseID, InstructorID, InstructorName).

o Functional Dependencies:

▪ StudentID -> CourseID (Each student is enrolled in specific courses)

▪ CourseID -> InstructorID, InstructorName (Each course is taught by a specific instructor)

4. Normal forms

• 1NF (First Normal Form):

o 1NF ensures that each attribute in a table contains only atomic (indivisible) values, and there are no
repeating groups or arrays within rows.

o Steps:

i.Place all items that appear in the repeating group in a new table

ii.Designate a primary key for each new table produced.

iii.Duplicate in the new table the primary key of the table from which the repeating group was extracted or vice versa.

o Example: A table adhering to 1NF has single values in each cell and avoids storing multiple values in a
single field.

• 2NF (Second Normal Form):

o 2NF builds upon 1NF and ensures that no partial dependencies exist, i.e Every non-key attribute is
fully functionally dependent on the entire primary key.

o Steps:

.If a data item is fully functionally dependent on only a part of the primary key, move that data item and that part of
the primary key to a new table.

i.If other data items are functionally dependent on the same part of the key, place them in the new table also

ii.Make the partial primary key copied from the original table the primary key for the new table. Place all items that
appear in the repeating group in a new table

o Example: In 2NF, each non-key attribute depends on the entire composite primary key, removing
partial dependencies.

• 3NF (Third Normal Form):

o 3NF further eliminates transitive dependencies by ensuring that no non-key attribute depends on
another non-key attribute within the same table.

o Steps:

.Move all items involved in transitive dependencies to a new entity.

i.Identify a primary key for the new entity.

ii.Place the primary key for the new entity as a foreign key on the original entity.

o Example: It resolves dependencies between non-key attributes, preventing indirect relationships.

• BCNF (Boyce-Codd Normal Form):

o BCNF is a stricter version of 3NF and eliminates all non-trivial functional dependencies where the
determinant is not a candidate key.
35
o Steps:

.Place the two candidate primary keys in separate entities

i.Place each of the remaining data items in one of the resulting entities according to its dependency on the primary
key.

o Example: In BCNF, every non-trivial functional dependency implies that the determinant is a
superkey.

5. Decomposition

• If decomposition does not cause any loss of information it is called a lossless decomposition.

• If a decomposition does not cause any dependencies to be lost it is called a dependency-preserving


decomposition.

• Any table scheme can be decomposed in a lossless way into a collection of smaller schemas that are in BCNF
form. However, the dependency preservation is not guaranteed.

• Any table can be decomposed in a lossless way into 3rd normal form that also preserves the dependencies.

o 3NF may be better than BCNF in some cases

36

You might also like