Professional Documents
Culture Documents
DBMS 1-4
DBMS 1-4
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 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.
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.
o Multiple views enhance usability by tailoring data presentation based on different user roles or
requirements.
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
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.
o Data within databases is logically coherent, organized in a structured manner following predefined
rules and relationships.
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.
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.
2
5. Users of database
• These individuals directly interact with, control, or use the database and its content:
▪ 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:
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.
o Oversee central control of data and programs accessing the data within DBMS.
• These individuals contribute to the development, implementation, and maintenance of the database system:
o Tool Developers:
▪ Design and implement software tools facilitating database modeling, design, system
optimization, and performance enhancement.
▪ Responsible for running and maintaining the hardware and software environment for the
database system.
• External Schema:
o Offers specific data formats & access rights unique to each user's needs
• Conceptual Schema:
o Ensures consistency across different user perspectives without detailing specific user needs.
• Internal Schema:
▪ 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:
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.
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
• Components:
o Authorization and Integrity Manager: Enforces integrity constraints and manages user access rights.
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 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.
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.
▪ 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.
▪ 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.
Chapter 2
• 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:
3. Attributes
• 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:
• Multivalued Attributes:
• 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 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.
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.
o Total participation involves every entity within an entity set participating in at least one relationship
within the relationship set.
• 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.
• 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.
• 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 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 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 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.
• 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.
o Constraints: Enforces rules and restrictions on data integrity, such as primary keys, foreign keys, and
unique constraints.
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
• Integrity Independence
• Distribution Independence
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 :
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.
14
S.No. RDBMS ORDBMS
9. It is capable of handling only simple data. It is also capable of handling the complex data.
• 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.
o SELECT (σ):
o RENAME (ρ):
13. Select
• Syntax: σ<condition>(Relation)
14. Project
• Syntax: π<attribute(s)>(Relation)
15. Rename
• Syntax: ρ<NewName/Attribute>(OldName)
• Example: ρ(NewSalary)(Salary)
• 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.
• 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.
• Functionality: Subtracts tuples from one relation that also exist in another.
• 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
• 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.
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
• Functionality: Merges rows from two relations where specified attributes are equal.
• 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.
• Purpose: Performs a JOIN operation based on common attributes between two relations.
• Functionality: Merges rows from two relations with matching attribute names.
• 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.
• Syntax: AggregateFunction<Attribute>(Relation)
18
• Result: Returns a single value that represents the result of the aggregate function applied to the specified
attribute in the relation.
• 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.
• 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:
• Transaction control:
o SQL includes commands for specifying the beginning and ending of transactions.
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
• numeric(p,d): Fixed point number with precision p and d digits to the right of the decimal point.
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:
• ALTER Command:
o Syntax for adding a column: ALTER TABLE table_name ADD column_name datatype;
• TRUNCATE Command:
o Removes all records from a table but keeps the table structure intact.
o Faster than DELETE as it doesn't generate individual deletion logs for each row.
• RENAME Command:
4. DML commands
• SELECT Command:
20
o Fundamental command used to retrieve data from a database.
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.
• INSERT Command:
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 Syntax: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
o The WHERE clause is crucial to specify which records to update; without it, all records in the table
might be affected.
• DELETE Command:
o Requires the WHERE clause to prevent accidentally deleting all records and to specify which records
to remove based on certain conditions.
• Where:
o The where clause specifies conditions that the result must satisfy
o select loan_number from loan where branch_name = ‘ Perryridge’ and amount > 1200
• From:
5. Constraints
21
• Key Constraints:
o Primary Key Constraint: Ensures uniqueness and non-null values in a column or a set of columns.
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.
• Domain Constraints:
• Referential Integrity:
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 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
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 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 Similar parameters to the GRANT command for specifying privilege, object, and user/role.
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;
▪ 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;
▪ 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 Example:
▪ BEGIN TRANSACTION;
23
▪ -- Execute SQL statements
▪ 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 Explanation: Concatenation allows combining strings together, useful for creating full names,
addresses, or custom messages within SQL queries by joining multiple string values.
o UPPER() and LOWER() are SQL functions used to convert strings to uppercase and lowercase,
respectively.
o Syntax:
o Example:
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 Example:
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:
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:
UNION
o Example:
UNION
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 INTERSECT
o Example:
o INTERSECT
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 EXCEPT
o Example:
o EXCEPT
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.
• 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:
o Usage:
• 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 FROM table_name
26
o GROUP BY column_name;
o Example:
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 FROM table_name
o GROUP BY column_name
o HAVING condition;
o Example:
o FROM employees
o GROUP BY department
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 FROM table_name
o WHERE condition;
o Example:
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 FROM table_name
o WHERE condition;
o Example:
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 FROM table1
o Example:
28
o SELECT employees.name, departments.department_name
o FROM employees
o Explanation: INNER JOIN retrieves rows from both "employees" and "departments" tables where the
"department_id" column matches, combining related information from both tables.
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 FROM table1
o Example:
o FROM employees
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.
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 FROM table1
o Example:
o FROM employees
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.
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 Example:
o FROM employees
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 FROM table1
o Example:
o FROM employees
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:
30
• BEGIN
• -- SQL statements
• END;
• Example:
• BEGIN
• 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.
• Example:
• Explanation: This command drops the trigger named "update_salary_trigger" if it exists in the database.
• 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.
• Authorization:
o GRANT Statement: Allows users or roles specific permissions to perform operations on database
objects.
o Example:
31
o REVOKE UPDATE ON table_name FROM user_name;
• Types of Permissions:
• 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 Restricted Access: Views can restrict access to sensitive data by displaying only specific columns or
rows to certain users or roles.
o Example:
o SELECT sensitive_column
o FROM sensitive_table
o WHERE condition;
Chapter 4
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.
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.
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 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.
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.
o Transitive Dependency:
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:
4. Normal forms
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
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.
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.
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:
ii.Place the primary key for the new entity as a foreign key on the original entity.
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:
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.
• 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.
36