0% found this document useful (0 votes)
25 views37 pages

Nots RDBMS

Notes rdbms

Uploaded by

Mahesh Mutnale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views37 pages

Nots RDBMS

Notes rdbms

Uploaded by

Mahesh Mutnale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit – 1 Basics of RDBMS (15 hrs.

)
1.1Characteristics of database approach
 Data Independence:
 Logical Independence: Applications are insulated from changes in the logical
structure of the data.
 Physical Independence: Applications are insulated from changes in the physical
storage structure of the data.
 Data Integration:
 Integration of data from multiple sources into a single, unified database, which
provides a consistent view of data.
 Data Integrity:
 Ensuring the accuracy, consistency, and validity of data stored in the database.
 Data Security:
 Implementation of measures to protect the database from unauthorized access, data
corruption, and loss.
 Concurrent Access and Transaction Control:
 Managing simultaneous access to the database by multiple users or applications
without interference.
 Ensuring the ACID properties (Atomicity, Consistency, Isolation, Durability) for
transactions.
 Data Modeling:
 Using conceptual, logical, and physical data models to design the database structure
that fits the requirements of the organization.
 Query Language:
 Providing a standardized language (e.g., SQL) for users and applications to retrieve
and manipulate data stored in the database.
 Backup and Recovery:
 Establishing procedures and mechanisms for backing up data regularly and recovering
data in case of hardware failures, human errors, or other disasters.
 Scalability and Performance:
 Designing the database system to efficiently handle increasing amounts of data and
user requests while maintaining acceptable performance levels.
 Data Warehousing and Analytics:
 Supporting the extraction, transformation, and loading (ETL) of data into data
warehouses for business intelligence and decision-making purposes.
 Database Administration:
 Responsibilities of database administrators (DBAs) in managing and maintaining
databases, ensuring availability, security, and performance.
 Normalization:
 Applying normalization techniques to reduce redundancy and improve data integrity
in relational database designs.

1.2 advantages and disadvantages of DBMS,


Advantages:
1. Data Sharing:
o DBMS allows multiple users and applications to access the same database
concurrently. This facilitates sharing of data across different functional areas
within an organization.
2. Data Integrity and Security:
o DBMS provides mechanisms for enforcing data integrity constraints (e.g.,
unique constraints, referential integrity) and implementing security measures
(e.g., access control, encryption) to protect data from unauthorized access and
corruption.
3. Data Independence:
o DBMS separates the logical structure of the database from its physical storage,
providing both logical and physical data independence. This means
applications can be insulated from changes in the database schema and storage
structures.
4. Efficient Data Access:
o DBMS optimizes data access and retrieval through indexing, query
optimization, and caching mechanisms, which improve performance and
reduce response times.
5. Concurrency Control:
o DBMS manages concurrent access to the database by multiple users and
applications, ensuring transactions are executed in isolation (Isolation property
of ACID) and maintaining data consistency.
6. Data Backup and Recovery:
o DBMS provides features for backing up data periodically and recovering data
in case of hardware failures, errors, or disasters, which enhances data
availability and reliability.
7. Scalability:
o DBMS systems are designed to handle large volumes of data and support
growth in data and user base without sacrificing performance, making them
scalable solutions for organizations.
8. Data Consistency:
o DBMS ensures that data stored in the database remains consistent over time by
enforcing constraints and maintaining atomicity, consistency, isolation, and
durability (ACID properties) for transactions.
Disadvantages:
1. Complexity:
o Designing and managing a DBMS can be complex and requires specialized
skills and knowledge. This complexity increases with the size and complexity
of the database and the organization's needs.
2. Cost:
o Implementing and maintaining a DBMS can be costly due to licensing fees,
hardware requirements, and ongoing maintenance and support costs. Small
organizations may find it challenging to afford or justify the expense.
3. Performance Overhead:
o DBMS imposes overhead in terms of processing and memory resources,
especially for complex queries and large-scale transactions. Poorly designed
databases or inefficient queries can degrade performance.
4. Security Risks:
o Despite security measures, DBMS can be vulnerable to security threats such as
SQL injection attacks, unauthorized access, and insider threats. Constant
vigilance and updates are required to mitigate these risks.
5. Dependency on Vendor:
o Organizations using a proprietary DBMS may become dependent on the
vendor for updates, support, and compatibility with other software and
hardware systems. Vendor lock-in can limit flexibility and increase costs.
6. Potential Single Point of Failure:
o If the DBMS fails or experiences downtime, it can disrupt operations across
the organization, affecting multiple applications and users. Redundancy and
disaster recovery plans are necessary to minimize such risks.
7. Complex Migration:
o Migrating data from one DBMS to another or upgrading to a new version can
be complex and time-consuming, requiring careful planning, testing, and
potential downtime.

1.3 Data models: Hierarchical, Network, Relational


1. Hierarchical Data Model
The hierarchical data model organizes data in a tree-like structure. Each record has a single
parent and zero or more children. This model is efficient for certain types of queries but can
be inflexible because of its strict hierarchy.
Example: Organizational Structure
Entities:
 Company
 Department
 Employee
Hierarchy:
Company
├── Department
│ ├── Employee
│ ├── Employee
├── Department
├── Employee
├── Employee
Sample Data:
 Company: ABC Corp
o Department: Sales
 Employee: John
 Employee: Jane
o Department: Engineering
 Employee: Alice
 Employee: Bob
Example Representation: In a hierarchical database like IBM's IMS, this could be
represented as:
Company(ABC Corp)
Department(Sales)
Employee(John)
Employee(Jane)
Department(Engineering)
Employee(Alice)
Employee(Bob)
2. Network Data Model
The network data model is a more flexible way of representing relationships. It allows each
record to have multiple parent and child records, forming a graph structure rather than a tree.
This model supports many-to-many relationships directly.
Example: University Course Registration
Entities:
 Student
 Course
 Instructor
Relationships:
 A student can enroll in multiple courses.
 A course can have multiple students.
 An instructor can teach multiple courses.
 A course can have multiple instructors.
Diagram:
Student <-- Enrolls --> Course
Course <-- TaughtBy --> Instructor
Sample Data:
 Student: Alice, Bob
 Course: Math101, CS101
 Instructor: Dr. Smith, Dr. Johnson
Relationships:
 Alice enrolls in Math101 and CS101.
 Bob enrolls in CS101.
 Dr. Smith teaches Math101.
 Dr. Johnson teaches CS101.
Example Representation: In a network database like IDMS, this could be represented using
sets:
SET Enrolls
Owner: Course
Members: Student

SET TaughtBy
Owner: Instructor
Members: Course
3. Relational Data Model
The relational data model organizes data into tables (relations), where each table consists of
rows (tuples) and columns (attributes). Tables can be related to each other using foreign keys.
This model is highly flexible and widely used due to its simplicity and robustness.
Example: E-commerce Application
Entities and Attributes:
 Customer: CustomerID, Name, Email
 Order: OrderID, OrderDate, CustomerID
 Product: ProductID, ProductName, Price
 OrderItem: OrderItemID, OrderID, ProductID, Quantity
Relationships:
 A customer can place multiple orders.
 An order can have multiple order items.
 An order item is associated with one product.
Tables:
CREATE TABLE Customer (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);

CREATE TABLE Order (


OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);

CREATE TABLE Product (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);

CREATE TABLE OrderItem (


OrderItemID INT PRIMARY KEY,
OrderID INT,
ProductID INT,
Quantity INT,
FOREIGN KEY (OrderID) REFERENCES Order(OrderID),
FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);
Example Data:
 Customer:
o CustomerID: 1, Name: John Doe, Email: [email protected]
 Order:
o OrderID: 1001, OrderDate: 2023-07-06, CustomerID: 1
 Product:
o ProductID: 2001, ProductName: Laptop, Price: 999.99
o ProductID: 2002, ProductName: Mouse, Price: 19.99
 OrderItem:
o OrderItemID: 3001, OrderID: 1001, ProductID: 2001, Quantity: 1
o OrderItemID: 3002, OrderID: 1001, ProductID: 2002, Quantity: 2

1.4 Schema and Instances


Schema:
 The schema is the structure or design of a database. It defines how the data is
organized and how the relationships between data are maintained. The schema
includes definitions of tables, columns, data types, constraints, and relationships.
 The schema is typically static and does not change frequently.
Instance:
 An instance is the actual data stored in the database at a particular moment in time. It
is a snapshot of the data in the database based on the schema.
 Instances change frequently as data is added, deleted, or updated.
Example: Library Database
Schema
Let's design a simple schema for a library database.
Tables:
1. Books
o BookID (Primary Key)
o Title
o Author
o Publisher
o YearPublished
2. Members
o MemberID (Primary Key)
o Name
o Address
o Email
3. Loans
o LoanID (Primary Key)
o BookID (Foreign Key)
o MemberID (Foreign Key)
o LoanDate
o ReturnDate
Schema Definition:
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(255),
Author VARCHAR(255),
Publisher VARCHAR(255),
YearPublished INT
);

CREATE TABLE Members (


MemberID INT PRIMARY KEY,
Name VARCHAR(255),
Address VARCHAR(255),
Email VARCHAR(255)
);

CREATE TABLE Loans (


LoanID INT PRIMARY KEY,
BookID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),
FOREIGN KEY (MemberID) REFERENCES Members(MemberID)
);
Instance
Let's populate the library database with some sample data. This is the instance of the database
at a specific point in time.
Books Table:
BookID Title Author Publisher YearPublished
1 "To Kill a Mockingbird" Harper Lee J.B. Lippincott 1960
2 "1984" George Orwell Secker & Warburg 1949
3 "The Great Gatsby" F. Scott Fitzgerald Scribner 1925
Members Table:
MemberID Name Address Email
1 John Doe 123 Elm St [email protected]
2 Jane Smith 456 Oak St [email protected]
3 Alice Johnson 789 Pine St [email protected]
Loans Table:
LoanID BookID MemberID LoanDate ReturnDate
1 1 1 2024-07-01 2024-07-15
2 2 2 2024-07-03 2024-07-17
3 3 3 2024-07-05 2024-07-19
In this example:
 The schema defines the structure of the Books, Members, and Loans tables.
 The instance is the actual data in these tables at a given point in time, as shown in the
sample data.
Difference
Schema
1. Definition:
o The schema is the overall design or structure of the database. It defines how
the data is organized and how the relationships among the data are associated.
2. Nature:
o The schema is static and does not change frequently. It is the blueprint of the
database.
3. Components:
o The schema includes definitions of tables, fields, data types, constraints,
relationships, views, indexes, and other elements that define the structure of
the database.
4. Purpose:
o The schema serves as a blueprint for how data is stored, accessed, and
managed in the database. It ensures data integrity and defines rules for data
consistency.
5. Example:
o In a library database, the schema would define tables like Books, Members,
and Loans along with their columns, data types, primary keys, and foreign
keys.
Instance
1. Definition:
o An instance refers to the actual data stored in the database at a particular
moment in time. It is a snapshot of the data based on the schema.
2. Nature:
o The instance is dynamic and changes frequently as data is added, deleted, or
updated in the database.
3. Components:
o The instance consists of the actual data records in the database tables.
4. Purpose:
The instance represents the state of the database at a specific time, showing the
o
current values stored in the database.
5. Example:
o In a library database, an instance would include the actual data such as:
 Books table: contains records of individual books.
 Members table: contains records of library members.
 Loans table: contains records of which books have been borrowed by
which members and their due dates.
Key Differences
Aspect Schema Instance
The overall design or structure of the The actual data stored in the database
Definition
database at a particular moment
Dynamic, changes frequently with
Nature Static, rarely changes
data operations
Tables, fields, data types, constraints, Actual data records in the database
Components
relationships, views, indexes tables
Blueprint for data storage, access, and Represents the current state of the
Purpose
management database
Definitions of Books, Members, Loans Records in the Books, Members,
Example
tables Loans tables at a specific time
Practical Example
Let's revisit the library database example:
Schema (Design):
sql
Copy code
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(255),
Author VARCHAR(255),
Publisher VARCHAR(255),
YearPublished INT
);

CREATE TABLE Members (


MemberID INT PRIMARY KEY,
Name VARCHAR(255),
Address VARCHAR(255),
Email VARCHAR(255)
);

CREATE TABLE Loans (


LoanID INT PRIMARY KEY,
BookID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),
FOREIGN KEY (MemberID) REFERENCES Members(MemberID)
);
Instance (Data):
-- Books table
INSERT INTO Books (BookID, Title, Author, Publisher, YearPublished) VALUES
(1, 'To Kill a Mockingbird', 'Harper Lee', 'J.B. Lippincott', 1960),
(2, '1984', 'George Orwell', 'Secker & Warburg', 1949),
(3, 'The Great Gatsby', 'F. Scott Fitzgerald', 'Scribner', 1925);

-- Members table
INSERT INTO Members (MemberID, Name, Address, Email) VALUES
(1, 'John Doe', '123 Elm St', '[email protected]'),
(2, 'Jane Smith', '456 Oak St', '[email protected]'),
(3, 'Alice Johnson', '789 Pine St', '[email protected]');
-- Loans table
INSERT INTO Loans (LoanID, BookID, MemberID, LoanDate, ReturnDate) VALUES
(1, 1, 1, '2024-07-01', '2024-07-15'),
(2, 2, 2, '2024-07-03', '2024-07-17'),
(3, 3, 3, '2024-07-05', '2024-07-19');
In this example:
 The schema defines the structure of the Books, Members, and Loans tables.
 The instance includes the actual records inserted into these tables at a specific time.

1.5 DBMS architecture Internal, Conceptual, External


Database Management System (DBMS) architecture typically consists of several layers that
work together to efficiently manage and access data. Here’s a simplified breakdown of the
typical architecture:
1. Users: This is where interaction with the DBMS begins. Users can be categorized into
different types such as:
o Database Administrators (DBAs): Responsible for managing and
maintaining the overall database system.
o Database Designers: Design the structure and schema of the database.
o End Users: Users who interact with the database through applications or
interfaces.
2. Database Applications: These are the applications and programs that interact with
the DBMS to access and manipulate data. Examples include web applications,
desktop applications, mobile apps, etc.
3. DBMS Interface: The interface layer provides a means for users and applications to
send queries and commands to the DBMS. This interface can be:
o Command-line interfaces (CLI): Text-based interfaces where users can type
commands.
o Graphical User Interfaces (GUI): Visual interfaces that allow users to
interact with the database using buttons, forms, and menus.
o Application Programming Interfaces (APIs): Interfaces that allow
applications to communicate directly with the DBMS programmatically.
4. Query Processor: When a query or command is received from the user or
application, the query processor interprets and optimizes it for efficient execution. It
includes:
o Query Parser: Breaks down the query into understandable components.
o Query Optimizer: Determines the most efficient way to execute the query
based on factors such as indexes, statistics, and data distribution.
5. Database Engine: This is the core of the DBMS. It includes several components:
o Storage Manager: Manages how data is stored and retrieved on disk.
o Buffer Manager: Caches data in memory to minimize disk I/O.
o Transaction Manager: Ensures ACID properties (Atomicity, Consistency,
Isolation, Durability) of transactions.
o File Manager: Handles the physical storage of data on disk.
o Index Manager: Manages indexes for quick data retrieval.
6. Database Storage: Data is stored physically on disk in files managed by the file
manager. Various data structures like tables, indexes, views, etc., are stored in these
files.
7. Database Schema: Defines the structure of the database, including tables, fields,
relationships, constraints, and indexes.
8. Data Access: The DBMS provides mechanisms for users and applications to access
and manipulate data according to the permissions and constraints defined in the
database schema.
9. Security and Authorization: DBMS provides mechanisms to ensure that only
authorized users and applications can access the database and that data remains
secure.
10. Concurrency Control: Ensures that multiple transactions can execute concurrently
without causing data inconsistency or integrity issues.
11. Backup and Recovery: DBMS includes facilities for backing up data periodically
and recovering data in case of hardware failures, crashes, or other disasters.
12. Transaction Management: Ensures that transactions are executed reliably and in
accordance with the ACID properties.

Three Schema Architecture


The three-tier architecture for Database Management Systems (DBMS) is a model that
separates the DBMS into three logically distinct layers, each serving a specific purpose. This
architecture is designed to improve scalability, flexibility, and manageability of database
systems. Here’s a breakdown of the three-tier DBMS architecture:
1. Presentation Tier (Client Tier):
o This is the topmost layer that interacts directly with users or applications. It
handles user interface and presentation logic.
o Responsibilities include gathering user input, displaying query results, and
formatting data for user interaction.
o Examples of interfaces in this tier include web browsers, GUI applications,
and mobile apps.
2. Application Tier (Middle Tier):
o Also known as the logic tier or middle tier, this layer sits between the
presentation tier and the data tier.
o It encapsulates the application logic responsible for processing user requests,
executing business logic, and interfacing with the data tier.
o Application servers, web servers, and middleware components reside in this
tier.
o Provides an abstraction layer that separates the user interface from the
database and ensures security and scalability of the application.
3. Data Tier (Database Tier):
o This is the bottommost layer that manages the actual storage and retrieval of
data.
o It consists of the database itself, which could be relational (e.g., SQL
databases like MySQL, PostgreSQL) or non-relational (e.g., NoSQL databases
like MongoDB, Cassandra).
o The data tier handles tasks such as data storage, retrieval, indexing, querying,
and transaction management.
o Database servers, storage systems, and database management systems
(DBMS) reside in this tier.
Advantages of Three-Tier DBMS Architecture:
 Scalability: Each tier can be scaled independently based on demand. For example,
more application servers can be added to handle increased user traffic without
affecting the database layer.
 Flexibility: Changes in one tier do not necessarily impact the other tiers, allowing for
easier upgrades and maintenance.
 Security: By separating the layers, security measures can be implemented at each tier
to protect sensitive data and resources.
 Performance: Optimizations specific to each tier can be applied, improving overall
system performance and responsiveness.
 Manageability: Clear separation of concerns makes the system easier to manage,
debug, and maintain over time.
The Internal, Conceptual, and External schema architecture is a conceptual framework used
in database design and implementation to describe and manage data at different levels of
abstraction. This approach helps in separating the user views from the way data is physically
stored and managed internally. Let's delve into each schema:
1. Internal Schema:
 Description: The internal schema, also known as the physical schema, defines how
the data is stored physically in the storage devices (e.g., hard disks).
 Focus: It deals with the physical aspects of storing, accessing, and manipulating data.
 Concerns: Includes data storage format, indexing techniques, data compression
methods, and physical data structures (like B-trees or hash tables).
 Audience: Typically, database administrators and system designers are concerned
with the internal schema.
 Example: Specifies details like data block size, file organization (sequential, indexed,
etc.), and storage structures optimized for performance.
2. Conceptual Schema:
 Description: The conceptual schema represents the entire structure of the database
using a high-level conceptual data model (like Entity-Relationship Model).
 Focus: It focuses on describing the logical structure of the entire database for the
entire organization.
 Concerns: Defines entities, their attributes, and relationships without concern for how
the data will be stored or accessed physically.
 Audience: Database designers, analysts, and application developers work with the
conceptual schema.
 Example: Defines entities (like Customer, Product), their attributes (like Name, ID),
and relationships (like Customer buys Product).
3. External Schema (also called Views or User Schema):
 Description: The external schema defines how each group of end-users perceive the
organization of data.
 Focus: It focuses on specific user groups and their specific data access needs.
 Concerns: Specifies subsets of data and operations visible to different user groups or
applications.
 Audience: End-users, application programmers, and query writers interact with the
external schema.
 Example: Views defined for different user roles or applications, showing specific
columns or rows from tables, hiding sensitive information, or joining data from
multiple tables.
Relationships Between the Schemas:
 Mapping: There are mappings between these schemas. The external schema is
mapped to the conceptual schema, which in turn is mapped to the internal schema.
 Abstraction: Each schema provides a higher level of abstraction, allowing for
separation of concerns and flexibility in managing complex data systems.
 Independence: Changes in one schema do not necessarily affect the others, ensuring
modularity and ease of maintenance.

1.6 Data independence: Logical, Physical


Data independence in the context of database systems refers to the separation between how
data is logically perceived or conceptualized (logical level) and how it is physically stored or
accessed (physical level). There are two main types of data independence:
1. Logical Data Independence:
 Definition: Logical data independence refers to the capacity to change the conceptual
schema (logical schema) without having to change external schemas or application
programs.
 Focus: It allows modifications to the logical schema, such as adding or modifying
tables, changing relationships, or altering constraints, without affecting the
applications that rely on the data.
 Benefit: Provides flexibility and adaptability to accommodate changes in data
organization or structure without disrupting the user applications.
 Example: Suppose you decide to split a single table into two related tables to
normalize the database structure. With logical data independence, applications that
query the database do not need to be rewritten as long as external views and queries
remain unchanged.
2. Physical Data Independence:
 Definition: Physical data independence refers to the capacity to change the physical
schema (internal schema) without affecting the conceptual schema or external
schemas.
 Focus: It allows modifications to the physical storage and access methods without
altering the logical schema or the applications that use the data.
 Benefit: Enables optimization of data storage, indexing, partitioning, or performance
enhancements without requiring changes to the way data is logically organized or
presented to users.
 Example: If you decide to change the underlying storage structure from a B-tree
index to a hash index for performance reasons, applications interacting with the
database do not need to be aware of or modified for this change, as long as the
external schema remains unchanged.
Importance of Data Independence:
 Flexibility: Allows database administrators and designers to make changes to the
database system efficiently without disrupting user applications.
 Modularity: Promotes modular design principles by separating concerns between
data storage and data usage.
 Maintenance: Simplifies maintenance and evolution of database systems by reducing
the impact of changes on application programs and user interfaces.

1.7 Concept of RDBMS


Relational Database Management System (RDBMS) is a type of database management
system that stores data in a structured format, using rows and columns (like a table), to
establish relationships between stored data elements. Here are the key concepts and
characteristics of RDBMS:
1. Tables, Rows, and Columns:
 Tables: Data is organized into tables, where each table represents an entity or
relationship. Tables consist of rows (records) and columns (fields or attributes).
 Rows: Each row in a table represents a unique record or instance of data.
 Columns: Columns define the attributes or properties of the data stored in each row.
2. Structured Query Language (SQL):
 RDBMS uses SQL as its standard language for querying and manipulating data. SQL
(Structured Query Language) allows users to retrieve, insert, update, and delete data
from tables, as well as perform various operations such as joining tables, creating
views, and defining constraints.
3. Relationships:
 RDBMS establishes relationships between tables through keys (primary and foreign
keys). These relationships enforce data integrity and ensure consistency in the
database.
 Primary Key: A unique identifier for each row in a table.
 Foreign Key: A field in one table that refers to the primary key in another table,
establishing a link between the two tables.
4. ACID Properties:
 RDBMS systems ensure transactional reliability and data integrity through the ACID
properties:
o Atomicity: Transactions are either fully completed or fully rolled back
(undone).
o Consistency: Transactions bring the database from one valid state to another
valid state.
o Isolation: Transactions are isolated from each other until they are complete to
prevent interference.
o Durability: Once a transaction is committed, the changes are permanent and
survive system failures.
5. Data Integrity:
 RDBMS enforces data integrity using constraints such as primary keys, foreign keys,
unique constraints, and check constraints. These constraints ensure that data is
accurate, valid, and consistent.
6. Normalization:
 RDBMS supports normalization, a process of organizing data in tables to minimize
redundancy and dependency. Normalization helps improve data integrity and reduces
storage space.
7. Concurrency Control:
 RDBMS manages concurrent access to the database by multiple users or applications.
It uses locking mechanisms and transaction isolation levels to ensure data consistency
and prevent conflicts.
8. Scalability and Performance:
 RDBMS systems are designed to scale vertically (adding more resources to a single
server) and horizontally (distributing data across multiple servers). They optimize
performance through indexing, query optimization, and caching mechanisms.
9. Security:
 RDBMS systems provide robust security features such as authentication,
authorization, encryption, and auditing to protect sensitive data from unauthorized
access and ensure compliance with regulatory requirements.
Examples of RDBMS:
Popular examples of RDBMS include:
 MySQL: Open-source RDBMS widely used in web applications.
 PostgreSQL: Open-source object-relational database system known for its robustness
and extensibility.
 Oracle Database: Commercial RDBMS known for its scalability and comprehensive
feature set.
 SQL Server: Microsoft's RDBMS with strong integration with Microsoft products
and cloud services.

1.8 Terminologies: relation, attribute, domain, tuple, entities, Integrity Constraints (Domain, Entity,
Referential)
1. Relation:
 Definition: In the context of relational databases, a relation refers to a table that holds
data organized in rows (tuples) and columns (attributes).
 Usage: Each row represents a record (tuple) and each column represents an attribute
(field or property). Relations are fundamental to the relational model and are
commonly referred to as tables in database management systems.
2. Attribute:
 Definition: An attribute is a named column of a relation (table) that represents a
specific characteristic or property of the entity (record or tuple).
 Usage: Attributes correspond to fields in a record and define the types of data that can
be stored in each column. For example, in a "Student" relation, attributes could
include "StudentID," "Name," "Age," and "GPA."
3. Domain:
 Definition: Domain refers to the set of allowable values for an attribute.
 Usage: Each attribute in a relation is associated with a domain that specifies the type
of data it can hold (e.g., integer, string, date) and any constraints on the values (e.g.,
range limits, format).
4. Tuple:
 Definition: A tuple is a single record or row in a relation (table).
 Usage: Tuples represent individual instances or entities within the domain described
by the relation's schema. For example, in a "Customer" relation, each tuple would
represent a specific customer with attributes like "CustomerID," "Name," "Address,"
etc.
5. Entities:
 Definition: In the context of databases, entities are objects or things that are
represented in the database and have an independent existence.
 Usage: Entities are typically represented as tables in a relational database, where each
entity corresponds to a relation. For example, a "Product" entity would be represented
as a "Product" relation with attributes like "ProductID," "Name," "Price," etc.
6. Integrity Constraints:
Integrity constraints are rules or conditions that enforce the validity, accuracy, and
consistency of data in a relational database. There are several types of integrity constraints:
 Domain Constraints:
o Definition: Domain constraints define the allowable values for an attribute
based on its domain.
o Usage: Example constraints include data type (e.g., integer, string) and
constraints such as range limits or format (e.g., date format).
 Entity Integrity Constraints:
o Definition: Entity integrity constraints ensure that each tuple in a relation has
a unique and non-null primary key value.
o Usage: This constraint ensures that primary key attributes cannot have null
values and must be unique within the relation.
 Referential Integrity Constraints:
o Definition: Referential integrity constraints ensure the consistency between
related tables by maintaining referential relationships between tables.
o Usage: These constraints typically involve foreign keys that reference primary
keys in other tables. They ensure that a foreign key value in one table matches
an existing primary key value in another table or is null.

1.9 Entity Relationship Model


An Entity Relationship Model (ERM) is a conceptual blueprint for structuring and organizing
data within a database. It visually represents the relationships between different data entities
in a system, helping to ensure clarity and consistency in database design. Here are the main
components:
1. Entities: Objects or things within the system that have a distinct existence. For
example, in a university database, entities might include Students, Courses, and
Professors.
2. Attributes: Characteristics or properties of entities. For example, a Student entity
might have attributes like StudentID, Name, and DateOfBirth.
3. Relationships: Connections between entities that describe how they interact with
each other. For example, a Student might enroll in a Course, creating a relationship
between the Student and Course entities.
4. Primary Key: A unique identifier for each entity instance. For example, StudentID
for a Student entity.
5. Foreign Key: An attribute that creates a link between two entities, representing the
relationship between them. For example, a Course entity might have a ProfessorID
attribute linking it to the Professor entity.
Basic Steps to Create an ERM
1. Identify Entities: Determine the primary objects or concepts to be represented in the
database.
2. Define Relationships: Establish how entities are related to each other.
3. Determine Attributes: List the properties for each entity.
4. Assign Keys: Designate primary keys and foreign keys as needed.
5. Create the Diagram: Use standard notations to draw the ERM.
Example ERM for a Simple University Database
1. Entities:
o Student: StudentID (PK), Name, DateOfBirth
o Course: CourseID (PK), CourseName, Credits
o Professor: ProfessorID (PK), Name, Department
2. Relationships:
o Enrollment: A Student enrolls in a Course.
 Attributes: EnrollmentDate
 Keys: StudentID (FK), CourseID (FK)
o Teaches: A Professor teaches a Course.
 Keys: ProfessorID (FK), CourseID (FK)
ERM Diagram Representation
Here's a textual representation:
 Student (StudentID, Name, DateOfBirth)
o Enrolls in
 Course (CourseID, CourseName, Credits)
o Taught by
 Professor (ProfessorID, Name, Department)

1.10 Entity Relationships: one-one, one-many, many-one, many-many


Entity relationships describe how entities interact with each other in a database. Here are the
primary types of relationships with examples:
1. One-to-One (1:1)
In a one-to-one relationship, a single entity instance in one entity is related to a single entity
instance in another entity.
Example:
 Person and Passport
o Each person has one unique passport.
o Each passport is assigned to one unique person.
Diagram:
scss
Copy code
Person (PersonID) 1-----1 Passport (PassportID)
2. One-to-Many (1
)
In a one-to-many relationship, a single entity instance in one entity is related to multiple
entity instances in another entity.
Example:
 Professor and Course
o One professor can teach many courses.
o Each course is taught by one professor.
Diagram:
scss
Copy code
Professor (ProfessorID) 1-----∞ Course (CourseID)
3. Many-to-One (M:1)
This is essentially the reverse of a one-to-many relationship. Multiple instances of one entity
are related to a single instance of another entity.
Example:
 Course and Department
o Many courses can belong to one department.
o Each department has many courses.
Diagram:
scss
Copy code
Course (CourseID) ∞-----1 Department (DepartmentID)
4. Many-to-Many (M
)
In a many-to-many relationship, multiple instances of one entity are related to multiple
instances of another entity. These relationships are typically broken down into two one-to-
many relationships using a junction table.
Example:
 Student and Course
o Many students can enroll in many courses.
o Each course can have many students enrolled.
Diagram:
scss
Copy code
Student (StudentID) ∞-----∞ Course (CourseID)
In practice, this relationship is represented using a junction table (Enrollment) to handle the
many-to-many relationship:
scss
Copy code
Student (StudentID) 1-----∞ Enrollment (StudentID, CourseID) ∞-----1 Course
(CourseID)
Summary of Relationship Types:
1. One-to-One (1:1): A ↔ B
2. One-to-Many (1
): A ↔ Many B
3. Many-to-One (M:1): Many A ↔ B
4. Many-to-Many (M
): Many A ↔ Many B (resolved with a junction table)

1.11 Key: Super key, Composite Key, Candidate Key, Primary Key, Alternate Key or Secondary Key,
Foreign Key),
In database design, keys are crucial for identifying records uniquely within a table. Here are
the definitions and examples of different types of keys:
1. Super Key
A super key is any combination of columns that uniquely identifies a row in a table. It can
include additional columns beyond the minimum required to ensure uniqueness.
Example:
 In a table Students with columns (StudentID, Name, Email), possible super keys
are:
o (StudentID)
o (Email)
o (StudentID, Email)
o (StudentID, Name, Email)
2. Composite Key
A composite key is a key that consists of two or more columns to uniquely identify a row in a
table. This is necessary when a single column is not sufficient to ensure uniqueness.
Example:
 In a table Enrollments with columns (StudentID, CourseID, EnrollmentDate),
a composite key could be:
o (StudentID, CourseID)
3. Candidate Key
A candidate key is a minimal super key; it is a super key without any redundant attributes. It
uniquely identifies a row in a table and there can be multiple candidate keys in a table.
Example:
 In a table Students with columns (StudentID, Name, Email), possible candidate
keys are:
o (StudentID)
o (Email)
4. Primary Key
A primary key is a candidate key chosen by the database designer to uniquely identify rows
in a table. It must contain unique values and cannot contain null values.
Example:
 In a table Students with columns (StudentID, Name, Email), if StudentID is
chosen as the primary key, it ensures each student is uniquely identifiable.
5. Alternate Key (or Secondary Key)
An alternate key is any candidate key that is not chosen as the primary key. It is still capable
of uniquely identifying rows in the table.
Example:
 Continuing with the Students table example, if StudentID is chosen as the primary
key, then Email would be an alternate key.
Example Table: Students
StudentID Name Email
1 Alice [email protected]
2 Bob [email protected]
3 Charlie [email protected]
 Super Key: (StudentID), (Email), (StudentID, Email), (StudentID, Name, Email)
 Composite Key: Not applicable here since single columns are sufficient.
 Candidate Key: (StudentID), (Email)
 Primary Key: (StudentID)
 Alternate Key: (Email)
Example Table: Enrollments
StudentID CourseID EnrollmentDate
1 101 2024-01-15
2 102 2024-01-16
1 102 2024-01-17
 Super Key: (StudentID, CourseID), (StudentID, CourseID, EnrollmentDate)
 Composite Key: (StudentID, CourseID)
 Candidate Key: (StudentID, CourseID)
 Primary Key: (StudentID, CourseID)
 Alternate Key: None in this case as there is only one candidate key.
Characteristics of Foreign Keys:
1. Referential Integrity: Foreign keys help maintain referential integrity by ensuring that a
value in one table corresponds to an existing value in another table.
2. Relationships: They establish relationships between tables, such as one-to-one, one-to-
many, or many-to-many.
3. Cascading Actions: In some database systems, foreign keys can enforce cascading actions
like updates and deletions. For example, if a referenced row is deleted, the rows referencing
it can also be automatically deleted (cascading delete).
Example of Foreign Key
Consider two tables: Students and Enrollments.
Students Table
StudentID Name Email
1 Alice [email protected]
2 Bob [email protected]
Primary Key: StudentID
Courses Table
CourseID CourseName Credits
101 Math 3
102 Science 4
 Primary Key: CourseID
Enrollments Table
EnrollmentID StudentID CourseID EnrollmentDate
1 1 101 2024-01-15
2 2 102 2024-01-16
3 1 102 2024-01-17
 Primary Key: EnrollmentID
 Foreign Keys: StudentID references Students(StudentID), CourseID references
Courses(CourseID)
Explanation
 In the Enrollments table, StudentID is a foreign key that references StudentID in the
Students table. This ensures that every StudentID value in the Enrollments table
corresponds to an existing StudentID in the Students table.
 Similarly, CourseID in the Enrollments table is a foreign key that references CourseID in
the Courses table.
Creating Foreign Keys in SQL
Here is how you can create these tables and define foreign keys in SQL:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);

CREATE TABLE Courses (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
Credits INT
);

CREATE TABLE Enrollments (


EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
Cascading Actions
You can define cascading actions on foreign keys to handle updates and deletions:
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID) ON DELETE
CASCADE ON UPDATE CASCADE,
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID) ON DELETE CASCADE
ON UPDATE CASCADE
);
In this example, if a student or a course is deleted, the corresponding rows in the
Enrollments table will also be deleted due to the ON DELETE CASCADE clause. Similarly, if
the StudentID or CourseID is updated, the changes will be reflected in the Enrollments
table because of the ON UPDATE CASCADE clause.
1.12 Normalization: 1Nf, 2NF, 3NF
Normalization is a database design technique used to minimize redundancy and avoid
anomalies in a relational database. It involves decomposing tables into smaller, well-
structured tables without losing data. The main normal forms are:
1. First Normal Form (1NF)
A table is in 1NF if:
 All columns contain atomic (indivisible) values.
 Each column contains values of a single type.
 Each column contains only one value per row (no repeating groups or arrays).
Example: Consider the following Students table that violates 1NF:
StudentID Name Courses
1 Alice Math, Science
2 Bob Math, English
To convert this table to 1NF, split the courses into individual rows:
StudentID Name Course
1 Alice Math
1 Alice Science
2 Bob Math
2 Bob English
2. Second Normal Form (2NF)
A table is in 2NF if:
 It is already in 1NF.
 All non-key attributes are fully functionally dependent on the primary key. (No partial
dependency on a composite primary key)
Example: Consider the following Enrollments table that violates 2NF:
StudentID CourseID StudentName CourseName
1 101 Alice Math
2 102 Bob Science
Here, StudentName and CourseName are partially dependent on StudentID and CourseID,
respectively. To convert this table to 2NF, decompose it into two tables:
Students Table:
StudentID StudentName
1 Alice
2 Bob
Courses Table:
CourseID CourseName
101 Math
102 Science
Enrollments Table:
StudentID CourseID
1 101
2 102
3. Third Normal Form (3NF)
A table is in 3NF if:
 It is already in 2NF.
 All non-key attributes are not only fully functionally dependent on the primary key
but also non-transitively dependent (i.e., no transitive dependency).
Example: Consider the following StudentDetails table that violates 3NF:
StudentID StudentName DeptID DeptName
1 Alice 10 Science
2 Bob 20 Arts
Here, DeptName is transitively dependent on StudentID through DeptID. To convert this
table to 3NF, decompose it into two tables:
Students Table:
StudentID StudentName DeptID
1 Alice 10
2 Bob 20
Departments Table:
DeptID DeptName
10 Science
20 Arts
Summary of Normal Forms:
 1NF: Eliminate repeating groups; ensure atomicity.
 2NF: Eliminate partial dependencies; ensure full functional dependency on the
primary key.
 3NF: Eliminate transitive dependencies; ensure that non-key attributes are only
dependent on the primary key.

1.13De-normalization
De-normalization is the process of intentionally introducing redundancy into a database by
merging tables or adding redundant data. This is done to improve read performance at the
cost of potential update anomalies and increased storage requirements. De-normalization can
help in scenarios where read operations are more frequent than write operations and where
performance is critical.
Why De-normalize?
1. Performance Improvement: Reducing the number of joins needed to retrieve data can
significantly speed up query performance.
2. Simpler Queries: De-normalized tables can make queries simpler and faster to write and
execute.
3. Caching: De-normalized tables can be used for caching purposes to avoid repetitive complex
joins.
Examples of De-normalization
Example 1: Combining Tables
Normalized Tables:
Orders Table:
OrderID CustomerID OrderDate
1 1001 2024-01-01
2 1002 2024-01-02
Customers Table:
CustomerID CustomerName
1001 Alice
1002 Bob
De-normalized Orders Table:
OrderID CustomerID CustomerName OrderDate
1 1001 Alice 2024-01-01
2 1002 Bob 2024-01-02
In this example, the Orders table now includes the CustomerName from the Customers table,
eliminating the need for a join when retrieving order information along with customer names.
Example 2: Adding Redundant Data
Normalized Tables:
Sales Table:
SaleID ProductID Quantity
1 101 2
SaleID ProductID Quantity
2 102 3
Products Table:
ProductID ProductName Price
101 Widget 25
102 Gadget 15
De-normalized Sales Table:
SaleID ProductID ProductName Price Quantity
1 101 Widget 25 2
2 102 Gadget 15 3
Here, the Sales table includes ProductName and Price from the Products table, so there's
no need to join these tables to get complete sales information.
Advantages of De-normalization
1. Performance Gains: Faster read queries due to reduced number of joins.
2. Simpler Queries: Queries become easier to write and understand.
3. Efficiency: Can be more efficient for reporting and data retrieval purposes.
Disadvantages of De-normalization
1. Update Anomalies: Increased risk of data inconsistencies when updating redundant data.
2. Increased Storage: More storage space is required to store redundant data.
3. Complexity in Writes: More complex logic may be needed to ensure data consistency during
write operations.
When to Use De-normalization
 High Read Operations: When the system experiences a high volume of read operations
compared to write operations.
 Complex Queries: When complex joins significantly impact performance.
 Data Warehousing: In data warehousing and reporting systems where data is often read-
heavy and less frequently updated.
1.14 Relational algebra.
Relational algebra is a formal system for manipulating relational databases. It consists of a set
of operations that take one or two relations as input and produce a new relation as output.
These operations are fundamental to the field of database theory and are used to query and
manipulate data stored in relational databases.
Basic Operations of Relational Algebra
1. Selection (σ)
2. Projection (π)
3. Union (∪)
4. Set Difference (-)
5. Cartesian Product (×)
6. Rename (ρ)
Advanced Operations
1. Intersection (∩)
2. Join (⨝)
3. Division (÷)
Basic Operations Explained
1. Selection (σ)
The selection operation selects rows from a relation that satisfy a given predicate.
Notation: σ<sub>condition</sub>(Relation)
Example: Select all students with age greater than 20.
σ Age > 20 (Students)
2. Projection (π)
The projection operation selects certain columns from a relation.
Notation: π<sub>columns</sub>(Relation)
Example: Select the names and ages of all students.
π Name, Age (Students)
3. Union (∪)
The union operation combines the tuples of two relations and removes duplicates.
Notation: Relation1 ∪ Relation2
Example: Combine two sets of students.
Students1 ∪ Students2
4. Set Difference (-)
The set difference operation finds tuples that are in one relation but not in the other.
Notation: Relation1 - Relation2
Example: Find students who are in Students1 but not in Students2.
Students1 - Students2
5. Cartesian Product (×)
The Cartesian product operation returns the Cartesian product of two relations.
Notation: Relation1 × Relation2
Example: Combine each student with each course.
Students × Courses
6. Rename (ρ)
The rename operation changes the name of a relation or its attributes.
Notation: ρ<sub>new_name</sub>(Relation) or
ρ<sub>new_name(columns)</sub>(Relation)
Example: Rename the Students relation to Pupils.
ρ Pupils (Students)
Advanced Operations Explained
1. Intersection (∩)
The intersection operation returns tuples that are in both relations.
Notation: Relation1 ∩ Relation2
Example: Find students who are in both Students1 and Students2.
Students1 ∩ Students2
2. Join (⨝)
The join operation combines related tuples from two relations into single tuples.
Notation: Relation1 ⨝<sub>condition</sub> Relation2
Example: Join Students and Enrollments on StudentID.
Students ⨝ Students.StudentID = Enrollments.StudentID Enrollments
Types of Joins:
 Theta Join (⨝<sub>θ</sub>): Join based on a general condition θ.
 Equi-Join: Join based on equality.
 Natural Join (⨝): Join based on all common attributes.
3. Division (÷)
The division operation is used to find tuples in one relation that are associated with all tuples
in another relation.
Notation: Relation1 ÷ Relation2
Example: Find students who are enrolled in all courses.
Students(StudentID, CourseID) ÷ Courses(CourseID)
Example Use Case
Consider the following relations:
Students:
StudentID Name Age
1 Alice 21
2 Bob 22
Enrollments:
StudentID CourseID
1 101
2 102
1 102
Courses:
CourseID CourseName
101 Math
102 Science
Query: Find the names of students who are enrolled in the Science course.
1. Join Enrollments and Courses:
Enrollments ⨝ Enrollments.CourseID = Courses.CourseID Courses
2. Select rows where CourseName is 'Science':
σ CourseName = 'Science' (Enrollments ⨝ Enrollments.CourseID =
Courses.CourseID Courses)
3. Join with Students to get names:
Students ⨝ Students.StudentID = Enrollments.StudentID σ CourseName =
'Science' (Enrollments ⨝ Enrollments.CourseID = Courses.CourseID
Courses)
4. Project the Name column:
π Name (Students ⨝ Students.StudentID = Enrollments.StudentID σ
CourseName = 'Science' (Enrollments ⨝ Enrollments.CourseID =
Courses.CourseID Courses))
This sequence of operations uses relational algebra to retrieve the names of students enrolled
in the Science course.

Unit – 2 Basics of MySQL (15 hrs.)


2.1 Features of MySQL
1. Open Source
 License: MySQL is open-source software under the GNU General Public License
(GPL), meaning it is free to use, and its source code is available for modification.
2. Cross-Platform Support
 Operating Systems: MySQL runs on various platforms, including Windows, Linux,
macOS, and UNIX.
3. SQL Compatibility
 SQL Standard: MySQL adheres to the ANSI SQL standard, ensuring compatibility
and ease of use for users familiar with SQL.
4. High Performance
 Storage Engines: MySQL offers multiple storage engines, such as InnoDB,
MyISAM, and Memory, allowing users to choose the one best suited for their needs.
 Query Optimization: MySQL includes features for query optimization, such as
indexing, query caching, and the query optimizer.
5. Scalability and Flexibility
 Vertical and Horizontal Scaling: MySQL supports both vertical scaling (adding
more resources to a single server) and horizontal scaling (adding more servers).
 Replication: MySQL offers replication features, including master-slave replication,
master-master replication, and group replication, for high availability and load
balancing.
6. Security
 Authentication and Authorization: MySQL provides robust user authentication and
authorization mechanisms.
 Encryption: Supports SSL/TLS for secure connections and data encryption for
enhanced security.
 Data Security: MySQL includes features like data encryption, access control, and
auditing.
7. Reliability and Availability
 ACID Compliance: MySQL's InnoDB storage engine is ACID-compliant, ensuring
reliable transactions with atomicity, consistency, isolation, and durability.
 Backup and Recovery: MySQL provides tools for backup and recovery, including
mysqldump, mysqlhotcopy, and third-party solutions.
8. Advanced Features
 Stored Procedures and Triggers: MySQL supports stored procedures, triggers, and
functions for enhanced database functionality.
 Views: Allows the creation of virtual tables (views) for simplified data access and
security.
 Partitioning: MySQL supports table partitioning to improve query performance and
manageability.
9. Connectivity
 Client-Server Architecture: MySQL operates on a client-server architecture,
supporting multiple clients connecting to a single server.
 APIs and Connectors: MySQL provides connectors and APIs for various
programming languages, including Java (JDBC), Python, PHP, C++, and .NET.
10. Full-Text Search
 Text Searching: MySQL supports full-text search capabilities, allowing users to
perform complex searches on text data.
11. JSON Support
 JSON Data: MySQL provides native support for JSON data types, enabling the
storage, indexing, and querying of JSON data.
12. Geographic Information System (GIS) Support
 Spatial Data: MySQL supports spatial data types and functions, allowing the storage
and manipulation of geographic data.
13. Community and Enterprise Editions
 MySQL Community Edition: The free, open-source version of MySQL.
 MySQL Enterprise Edition: A commercial version with additional features such as
enterprise-level security, scalability, and support services.
14. Data Warehousing and Big Data
 Integration: MySQL integrates with various data warehousing and big data solutions,
including Hadoop, Apache Spark, and others.
15. Administration and Management Tools
 MySQL Workbench: An integrated development environment (IDE) for database
design, administration, and SQL development.
 MySQL Enterprise Monitor: A tool for monitoring and managing MySQL servers.
16. Community and Support
 Community Support: MySQL has a large and active community, providing forums,
documentation, and third-party tools.
 Professional Support: Oracle provides professional support services for MySQL
Enterprise Edition.

2.2Data types
1. Numeric Data Types
Integer Types
 TINYINT: A very small integer. Range: -128 to 127 (signed) or 0 to 255 (unsigned).
 SMALLINT: A small integer. Range: -32,768 to 32,767 (signed) or 0 to 65,535 (unsigned).
 MEDIUMINT: A medium-sized integer. Range: -8,388,608 to 8,388,607 (signed) or 0 to
16,777,215 (unsigned).
 INT or INTEGER: A standard integer. Range: -2,147,483,648 to 2,147,483,647 (signed) or 0 to
4,294,967,295 (unsigned).
 BIGINT: A large integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
(signed) or 0 to 18,446,744,073,709,551,615 (unsigned).
Floating-Point Types
 FLOAT: A small (single-precision) floating-point number. Range: approximately
±1.175494351 E-38 to ±3.402823466 E+38.
 DOUBLE: A normal-size (double-precision) floating-point number. Range: approximately
±2.2250738585072014 E-308 to ±1.7976931348623157 E+308.
 DECIMAL: An exact fixed-point number. Allows for precise storage of decimal values. Syntax:
DECIMAL(M, D), where M is the maximum number of digits and D is the number of digits to
the right of the decimal point.
2. String Data Types
Character String Types
 CHAR: A fixed-length string. Syntax: CHAR(N), where N is the length of the string (0 to 255).
 VARCHAR: A variable-length string. Syntax: VARCHAR(N), where N is the maximum length of
the string (0 to 65,535).
Text Types
 TINYTEXT: A very small text string. Maximum length: 255 characters.
 TEXT: A small text string. Maximum length: 65,535 characters.
 MEDIUMTEXT: A medium-length text string. Maximum length: 16,777,215 characters.
 LONGTEXT: A large text string. Maximum length: 4,294,967,295 characters.
Binary Data Types
 BINARY: A fixed-length binary string. Syntax: BINARY(N), where N is the length of the string.
 VARBINARY: A variable-length binary string. Syntax: VARBINARY(N), where N is the
maximum length of the string.
Blob Types
 TINYBLOB: A very small binary large object. Maximum length: 255 bytes.
 BLOB: A small binary large object. Maximum length: 65,535 bytes.
 MEDIUMBLOB: A medium-sized binary large object. Maximum length: 16,777,215 bytes.
 LONGBLOB: A large binary large object. Maximum length: 4,294,967,295 bytes.
Enum and Set Types
 ENUM: An enumeration. A string object that can have only one value, chosen from a list of
permitted values. Syntax: ENUM('value1', 'value2', ...).
 SET: A set. A string object that can have zero or more values, each chosen from a list of
permitted values. Syntax: SET('value1', 'value2', ...).
3. Date and Time Data Types
 DATE: A date value. Format: 'YYYY-MM-DD'. Range: '1000-01-01' to '9999-12-31'.
 DATETIME: A date and time combination. Format: 'YYYY-MM-DD HH:MM
'. Range: '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
 TIMESTAMP: A timestamp. Format: 'YYYY-MM-DD HH:MM
'. Range: '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
 TIME: A time value. Format: 'HH:MM
'. Range: '-838:59:59' to '838:59:59'.
 YEAR: A year value. Format: 'YYYY'. Range: 1901 to 2155 (4-digit year) or 70 to 69 (2-digit
year representing 1970 to 2069).
4. Spatial Data Types
 GEOMETRY: A geometric object.
 POINT: A point in 2D space.
 LINESTRING: A series of points forming a line.
 POLYGON: A polygon.
 MULTIPOINT: A collection of points.
 MULTILINESTRING: A collection of lines.
 MULTIPOLYGON: A collection of polygons.
 GEOMETRYCOLLECTION: A collection of geometry objects.
5. JSON Data Type
 JSON: A JSON (JavaScript Object Notation) data type for storing JSON documents.
Examples
Creating a Table with Various Data Types
CREATE TABLE ExampleTable (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
birth_date DATE,
registered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
bio TEXT,
profile_image BLOB,
preferences JSON,
location POINT,
rating DECIMAL(3, 2)
);
This table includes various data types to demonstrate the flexibility of MySQL in handling
different kinds of data.

2.3 User management Database (Create, Use, Drop, Show, Copy)


Creating and managing databases in MySQL involves several operations, such as creating
databases, using databases, dropping databases, showing databases, and copying databases.
Here is a detailed guide on how to perform each of these operations:
1. Creating a Database
To create a database, you use the CREATE DATABASE statement.
CREATE DATABASE mydatabase;
2. Using a Database
To select and use a database, you use the USE statement.
USE mydatabase;
3. Dropping a Database
To delete a database, you use the DROP DATABASE statement.
DROP DATABASE mydatabase;
4. Showing Databases
To list all databases in the MySQL server, you use the SHOW DATABASES statement.
SHOW DATABASES;
5. Copying a Database
MySQL does not have a direct command to copy a database. However, you can achieve this
by creating a backup and then restoring it to a new database.
Method 1: Using mysqldump
1. Export the database to a SQL file:
mysqldump -u username -p mydatabase > mydatabase_backup.sql
2. Create a new database:
CREATE DATABASE mydatabase_copy;
3. Import the SQL file into the new database:
mysql -u username -p mydatabase_copy < mydatabase_backup.sql
Method 2: Using SQL Statements
1. Create a new database:
CREATE DATABASE mydatabase_copy;
2. Use the new database:
USE mydatabase_copy;
3. Copy each table individually:
For each table in the original database, use the following SQL command:
CREATE TABLE new_table LIKE old_table;
INSERT INTO new_table SELECT * FROM old_table;
Replace new_table with the new table name and old_table with the original table name.
Example Scenario
Let’s go through an example scenario where we create a database, use it, show the databases,
and drop the database.
Step 1: Create a Database
CREATE DATABASE UserManagement;
Step 2: Use the Database
USE UserManagement;
Step 3: Create a Table in the Database
CREATE TABLE Users (
UserID INT AUTO_INCREMENT PRIMARY KEY,
Username VARCHAR(50) NOT NULL,
Password VARCHAR(50) NOT NULL,
Email VARCHAR(100) NOT NULL,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 4: Insert Data into the Table
INSERT INTO Users (Username, Password, Email) VALUES ('john_doe',
'password123', '[email protected]');
INSERT INTO Users (Username, Password, Email) VALUES ('jane_doe',
'password456', '[email protected]');
Step 5: Show All Databases
SHOW DATABASES;
Step 6: Copy the Database (Using mysqldump method)
1. Export the database to a SQL file:
mysqldump -u root -p UserManagement > UserManagement_backup.sql
2. Create a new database:

CREATE DATABASE UserManagement_copy;


3. Import the SQL file into the new database:
mysql -u root -p UserManagement_copy < UserManagement_backup.sql
Step 7: Drop the Database

DROP DATABASE UserManagement;


This sequence covers the essential operations for creating, using, showing, copying, and
dropping databases in MySQL.

2.4 DDL
DDL stands for Data Definition Language, and it is a subset of SQL (Structured Query
Language) used to define and manage the structure of a database and its objects. DDL
statements are used to create, modify, and drop database objects such as tables, indexes,
views, and schemas. Here are the primary DDL statements in SQL:
1. CREATE
The CREATE statement is used to create database objects like tables, indexes, views, and
schemas.
 Create a Table:
CREATE TABLE Students (
StudentID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Age INT,
GPA DECIMAL(3, 2)
);
 Create an Index:
CREATE INDEX idx_lastname ON Students(LastName);
 Create a View:
CREATE VIEW StudentSummary AS
SELECT StudentID, FirstName, LastName, Age FROM Students WHERE GPA >=
3.5;
2. ALTER
The ALTER statement is used to modify the structure of an existing database object.
 Alter Table (Add Column):
ALTER TABLE Students ADD COLUMN Email VARCHAR(100);
 Alter Table (Modify Column):
ALTER TABLE Students MODIFY COLUMN Age INT UNSIGNED;
 Alter Table (Drop Column):
ALTER TABLE Students DROP COLUMN GPA;
3. DROP
The DROP statement is used to delete database objects like tables, indexes, views, and
schemas.
 Drop Table:
DROP TABLE Students;
 Drop Index:
DROP INDEX idx_lastname ON Students;
 Drop View:
DROP VIEW StudentSummary;
4. TRUNCATE
The TRUNCATE statement is used to remove all rows from a table, while keeping the table
structure intact.
 Truncate Table:
TRUNCATE TABLE Students;
5. RENAME
The RENAME statement is used to rename an existing database object.
 Rename Table:
RENAME TABLE Students TO NewStudents;
6. COMMENT ON
The COMMENT ON statement is used to add comments to database objects like tables and
columns.
 Add Comment to Table:
COMMENT ON TABLE Students IS 'Table storing information about
students.';
 Add Comment to Column:
COMMENT ON COLUMN Students.FirstName IS 'First name of the student.';
Example Usage
Let's go through an example scenario using some DDL statements:
-- Create a table
CREATE TABLE Employees (
EmployeeID INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100),
HireDate DATE
);

-- Alter the table to add a new column


ALTER TABLE Employees ADD COLUMN Department VARCHAR(50);

-- Insert data into the table


INSERT INTO Employees (FirstName, LastName, Email, HireDate, Department)
VALUES ('John', 'Doe', '[email protected]', '2023-01-15', 'IT'),
('Jane', 'Smith', '[email protected]', '2022-05-20', 'HR');

-- Create an index on Email column


CREATE INDEX idx_email ON Employees(Email);

-- Create a view to show employees hired in the last year


CREATE VIEW RecentEmployees AS
SELECT * FROM Employees WHERE HireDate >= CURDATE() - INTERVAL 1 YEAR;

2.5 DML
"DML" stands for Data Manipulation Language. It is a subset of SQL (Structured Query
Language) used for managing data within relational databases. DML statements are used to
retrieve, insert, update, and delete data from database tables. Here are the primary DML
statements in SQL:
1. SELECT
The SELECT statement retrieves data from one or more tables.
 Basic Select:
SELECT * FROM Employees;
 Select with Conditions:
SELECT FirstName, LastName FROM Employees WHERE Department = 'IT';
 Select with Joins:
SELECT e.FirstName, e.LastName, d.DepartmentName
FROM Employees e
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
2. INSERT
The INSERT statement is used to add new rows of data into a table.
 Insert into Table:
INSERT INTO Employees (FirstName, LastName, Email, HireDate,
Department)
VALUES ('Alice', 'Johnson', '[email protected]', '2023-03-
10', 'Finance');
3. UPDATE
The UPDATE statement modifies existing data in a table.
 Update Records:
UPDATE Employees
SET Department = 'Marketing'
WHERE EmployeeID = 101;
4. DELETE
The DELETE statement removes rows from a table.
 Delete Records:
DELETE FROM Employees
WHERE EmployeeID = 102;
Example Usage
Let's go through an example scenario using these DML statements:
-- Insert data into Employees table
INSERT INTO Employees (FirstName, LastName, Email, HireDate, Department)
VALUES ('John', 'Doe', '[email protected]', '2023-01-15', 'IT'),
('Jane', 'Smith', '[email protected]', '2022-05-20', 'HR');

-- Update an employee's department


UPDATE Employees
SET Department = 'Finance'
WHERE LastName = 'Smith';

-- Delete an employee record


DELETE FROM Employees
WHERE EmployeeID = 103;

-- Select data to view results


SELECT * FROM Employees;
Summary
DML statements are essential for interacting with and manipulating data stored in relational
databases. They provide the ability to retrieve specific information (SELECT), add new records
(INSERT), update existing data (UPDATE), and remove unwanted data (DELETE). These
operations allow developers and database administrators to manage and maintain the integrity
and accuracy of data within their database systems.

2.6 DCL
"DCL" stands for Data Control Language in the context of databases. It is a subset of SQL
(Structured Query Language) used to control access to data stored in a database. DCL
statements primarily include commands for granting and revoking privileges and permissions
to database users. The two main DCL statements in SQL are:
1. GRANT
The GRANT statement is used to give specific privileges to database users or roles.
 Grant SELECT privilege to a user:
sql
Copy code
GRANT SELECT ON Employees TO user1;
 Grant INSERT, UPDATE privileges to a role:
sql
Copy code
GRANT INSERT, UPDATE ON Employees TO role1;
2. REVOKE
The REVOKE statement is used to revoke previously granted privileges from database users or
roles.
 Revoke SELECT privilege from a user:
REVOKE SELECT ON Employees FROM user1;
 Revoke all privileges from a user:
REVOKE ALL PRIVILEGES ON Employees FROM user1;
Example Usage
Let's illustrate the usage of GRANT and REVOKE statements with a simple scenario:
-- Create a new user
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password';

-- Grant SELECT privilege on a table to the user


GRANT SELECT ON Employees TO 'user1'@'localhost';

-- Revoke INSERT privilege from the user


REVOKE INSERT ON Employees FROM 'user1'@'localhost';
In this example:
 We create a new user user1 with a password.
 We grant the SELECT privilege on the Employees table to user1.
 Later, we revoke the INSERT privilege from user1.

2.7 TCL Commands


TCL (Transaction Control Language) commands in SQL are used to manage transactions
within a database. Transactions are sequences of operations that are treated as a single unit of
work, ensuring data integrity and consistency. TCL commands help in controlling the
outcome of these transactions. The main TCL commands include:
1. COMMIT
The COMMIT command is used to permanently save the changes made during the current
transaction. Once committed, the changes become visible to other transactions.
 Commit a transaction:
COMMIT;
2. ROLLBACK
The ROLLBACK command is used to undo the changes made during the current transaction,
reverting the database to its state before the transaction began.
 Rollback a transaction:
ROLLBACK;
3. SAVEPOINT
The SAVEPOINT command sets a named point within the current transaction that can be used
later with ROLLBACK TO to revert the transaction to that point.
 Create a savepoint:
SAVEPOINT sp1;
4. ROLLBACK TO SAVEPOINT
The ROLLBACK TO SAVEPOINT command reverts the transaction to a named savepoint created
within the current transaction.
 Rollback to a savepoint:
ROLLBACK TO SAVEPOINT sp1;
Example Usage
Let's demonstrate how TCL commands are used in a transaction:
-- Begin a transaction
START TRANSACTION;

-- Insert data into a table


INSERT INTO Employees (FirstName, LastName, Email)
VALUES ('John', 'Doe', '[email protected]');

-- Create a savepoint
SAVEPOINT sp1;

-- Update data in the table


UPDATE Employees
SET Email = '[email protected]'
WHERE LastName = 'Doe';

-- Rollback to the savepoint


ROLLBACK TO SAVEPOINT sp1;

-- Commit the transaction


COMMIT;
In this example:
 We begin a transaction with START TRANSACTION.
 Insert data into the Employees table.
 Create a savepoint sp1 after the insertion.
 Update data in the table but decide to rollback to sp1 to revert the changes made since
the savepoint.
 Finally, commit the transaction to make the insertion permanent.

2.8 Clauses – Order by, where And group by


1. WHERE Clause
The WHERE clause is used to filter records based on specified conditions. It is used in SELECT,
UPDATE, DELETE, and MERGE statements to specify which rows to retrieve or modify.
 Basic Usage:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
 Example:
SELECT * FROM Employees
WHERE Department = 'IT';
This query retrieves all columns (*) from the Employees table where the Department
is 'IT'.
2. ORDER BY Clause
The ORDER BY clause is used to sort the result set of a SELECT statement in ascending
(default) or descending order based on one or more columns.
 Basic Usage:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
 Example:
SELECT * FROM Employees
ORDER BY LastName ASC, FirstName ASC;
This query retrieves all columns (*) from the Employees table and sorts them first by
LastName in ascending order (ASC), and then by FirstName in ascending order (ASC).
3. GROUP BY Clause
The GROUP BY clause is used in conjunction with aggregate functions (like SUM, AVG, COUNT,
MIN, MAX) to group rows that have the same values into summary rows.
 Basic Usage:
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
 Example:
SELECT Department, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department;
This query counts the number of employees (COUNT(*)) in each Department and
groups the results by Department.
Additional Considerations
 Filtering with WHERE: Use the WHERE clause to filter rows before grouping or
sorting them with GROUP BY or ORDER BY.
 Sorting with ORDER BY: The ORDER BY clause can sort data based on multiple
columns and can specify ascending (ASC) or descending (DESC) order for each
column.
 Grouping with GROUP BY: When using GROUP BY, every column in the SELECT
clause that is not aggregated (like COUNT, SUM, etc.) must be included in the GROUP BY
clause.
Example Combining Clauses
-- Example: List departments with more than 5 employees, ordered by
employee count
SELECT Department, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 5
ORDER BY EmployeeCount DESC;
In this example:
 GROUP BY Department groups the employees by Department.
 HAVING COUNT(*) > 5 filters departments with more than 5 employees.
 ORDER BY EmployeeCount DESC sorts the results in descending order based on
EmployeeCount.

2.9 Operators : Arithmetic(DIV, /, -, +, *, %, MOD)


In SQL, arithmetic operators are used to perform mathematical calculations on numeric data
within queries. Here are the common arithmetic operators supported in SQL:
1. Addition (+)
The addition operator (+) adds two numbers.
 Example:
SELECT 10 + 5; -- Result: 15
2. Subtraction (-)
The subtraction operator (-) subtracts one number from another.
 Example:
SELECT 20 - 8; -- Result: 12
3. Multiplication (*)
The multiplication operator (*) multiplies two numbers.
 Example:
SELECT 6 * 4; -- Result: 24
4. Division (/ or DIV)
The division operators (/ or DIV) divide one number by another.
 Example:
SELECT 15 / 3; -- Result: 5
SELECT 10 DIV 3; -- Result: 3 (Integer division)
5. Modulo (%) or MOD
The modulo operators (% or MOD) return the remainder of a division operation.
 Example:
SELECT 17 % 5; -- Result: 2 (17 divided by 5 gives a remainder of 2)

SELECT MOD(17, 5); -- Result: 2 (Using MOD function)


Example Usage in SQL Queries
Here's how you might use these arithmetic operators in SQL queries:
-- Calculate the total salary by adding basic salary and bonus
SELECT EmployeeID, FirstName, LastName, BasicSalary, Bonus,
BasicSalary + Bonus AS TotalSalary
FROM Employees;

-- Calculate the remaining stock quantity after selling some items


UPDATE Inventory
SET StockQuantity = StockQuantity - 10
WHERE ProductID = 123;

-- Calculate the average age of employees


SELECT AVG(Age) AS AverageAge
FROM Employees;

-- Calculate the discount price after applying a 20% discount


SELECT ProductName, Price, Price * 0.8 AS DiscountedPrice
FROM Products;
Notes:
 Parentheses () can be used to enforce operator precedence.
 SQL typically follows standard mathematical operator precedence rules (e.g.,
multiplication and division before addition and subtraction), but it's good practice to
use parentheses to make intentions clear in complex expressions.

2.10 Comparison operator (=, <>, >, =, <=)


In SQL, comparison operators are used to compare values in expressions and conditions
within queries. They allow you to retrieve data based on specified criteria. Here are the
common comparison operators supported in SQL:
1. Equal to (=)
The equal to operator (=) checks if two expressions are equal.
 Example:
SELECT * FROM Employees WHERE Department = 'IT';
This query retrieves all rows from the Employees table where the Department
column equals 'IT'.
2. Not equal to (<>)
The not equal to operator (<>) checks if two expressions are not equal.
 Example:
SELECT * FROM Employees WHERE Department <> 'Finance';
This query retrieves all rows from the Employees table where the Department
column is not equal to 'Finance'.
3. Greater than (>)
The greater than operator (>) checks if the left operand is greater than the right operand.
 Example:
SELECT * FROM Products WHERE Price > 100;
This query retrieves all rows from the Products table where the Price column is
greater than 100.
4. Greater than or equal to (>=)
The greater than or equal to operator (>=) checks if the left operand is greater than or equal to
the right operand.
 Example:
SELECT * FROM Orders WHERE OrderDate >= '2023-01-01';
This query retrieves all rows from the Orders table where the OrderDate column is
on or after January 1st, 2023.
5. Less than (<)
The less than operator (<) checks if the left operand is less than the right operand.
 Example:
SELECT * FROM Customers WHERE Age < 30;
This query retrieves all rows from the Customers table where the Age column is less
than 30.
6. Less than or equal to (<=)
The less than or equal to operator (<=) checks if the left operand is less than or equal to the
right operand.
 Example:
SELECT * FROM Products WHERE StockQuantity <= 10;
This query retrieves all rows from the Products table where the StockQuantity
column is less than or equal to 10.
Example Usage in SQL Queries
Here are a few examples demonstrating the use of comparison operators in SQL queries:
-- Select employees earning more than 50000
SELECT * FROM Employees WHERE Salary > 50000;

-- Select products with a price less than or equal to 50


SELECT * FROM Products WHERE Price <= 50;

-- Update the status of orders placed after January 1, 2023


UPDATE Orders SET Status = 'Shipped' WHERE OrderDate > '2023-01-01';

-- Delete customers aged 18 or younger


DELETE FROM Customers WHERE Age <= 18;
Notes:
 Comparison operators can be combined with logical operators (AND, OR, NOT) to form
complex conditions.
 String comparisons are case-insensitive in most SQL databases unless specified
otherwise.

2.11 Set operators : Union, Union all, Intersect, Minus


In SQL, set operators are used to combine the results of two or more SELECT statements.
They allow you to perform operations similar to set theory operations (union, intersection,
difference) on the result sets returned by queries. Here are the common set operators in SQL:
1. UNION
The UNION operator is used to combine the result sets of two or more SELECT statements. It
removes duplicate rows between the various SELECT statements.
 Basic Syntax:
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
 Example:
SELECT FirstName, LastName FROM Employees
UNION
SELECT FirstName, LastName FROM Customers;
This query combines the FirstName and LastName columns from the Employees and
Customers tables, removing duplicates.
2. UNION ALL
The UNION ALL operator is similar to UNION, but it does not remove duplicate rows between
the SELECT statements. It includes all rows from all result sets.
 Basic Syntax:
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
 Example:
SELECT FirstName, LastName FROM Employees
UNION ALL
SELECT FirstName, LastName FROM Customers;
This query combines the FirstName and LastName columns from the Employees and
Customers tables, including all rows, even duplicates.
3. INTERSECT
The INTERSECT operator returns the intersection (common rows) of the result sets of two or
more SELECT statements.
 Basic Syntax:
SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;
 Example:
SELECT FirstName, LastName FROM Employees
INTERSECT
SELECT FirstName, LastName FROM Customers;
This query returns the FirstName and LastName columns that are common between
the Employees and Customers tables.
4. MINUS (or EXCEPT)
The MINUS (or EXCEPT) operator returns the difference between the result sets of two SELECT
statements. It retrieves rows from the first query that are not present in the second query.
 Basic Syntax:
SELECT column1, column2, ...
FROM table1
MINUS
SELECT column1, column2, ...
FROM table2;
 Example:
SELECT FirstName, LastName FROM Employees
MINUS
SELECT FirstName, LastName FROM Customers;
This query returns the FirstName and LastName columns from the Employees table
that are not present in the Customers table.
Example Usage in SQL Queries
Here are some scenarios demonstrating the use of set operators in SQL:
-- Union of results from two tables
SELECT ProductName FROM Products
UNION
SELECT ProductName FROM DiscontinuedProducts;

-- Union all including duplicates


SELECT ProductName FROM Products
UNION ALL
SELECT ProductName FROM Products WHERE Price > 100;

-- Intersection of two tables


SELECT CustomerID FROM Customers
INTERSECT
SELECT CustomerID FROM Orders;

-- Difference between two tables


SELECT EmployeeID FROM Employees
MINUS
SELECT EmployeeID FROM Managers;
Notes:
 Each SELECT statement within a set operation must have the same number of columns
with compatible data types.
 UNION, INTERSECT, and MINUS are typically used to combine or compare data from
similar tables or result sets.
 UNION ALL is useful when you want to include all rows, including duplicates, from
multiple result sets.

2.12 Other Operator: like, in, not, between, exists, all, any, is null, is not null, distinct.
Certainly! In SQL, there are various other operators and keywords that are used for different
purposes such as comparison, filtering, and checking conditions. Here's an explanation of
each of these operators and keywords:
1. LIKE
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
 Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
 Example:
SELECT * FROM Employees
WHERE FirstName LIKE 'J%';
This query retrieves all rows from the Employees table where the FirstName starts
with 'J'.
 Wildcard Characters:
o %: Represents zero or more characters.
o _: Represents a single character.
2. IN
The IN operator allows you to specify multiple values in a WHERE clause.
 Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
 Example:
SELECT * FROM Products
WHERE Category IN ('Electronics', 'Clothing', 'Books');
This query retrieves all rows from the Products table where the Category is either
'Electronics', 'Clothing', or 'Books'.
3. NOT
The NOT operator reverses the result of a condition.
 Example:
SELECT * FROM Employees
WHERE NOT Department = 'IT';
This query retrieves all rows from the Employees table where the Department is not
'IT'.
4. BETWEEN
The BETWEEN operator selects values within a specified range.
 Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
 Example:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
This query retrieves all rows from the Orders table where the OrderDate is between
January 1st, 2023 and December 31st, 2023.
5. EXISTS
The EXISTS operator is used in a WHERE clause to test for the existence of any rows in a
subquery.
 Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE EXISTS (subquery);
 Example:
SELECT * FROM Customers
WHERE EXISTS (
SELECT 1 FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID
);
This query retrieves all rows from the Customers table where there exists at least one
corresponding row in the Orders table for that customer.
6. ALL and ANY/IN
The ALL and ANY (synonymous with IN) operators are used with subqueries and comparison
operators (=, >, <, etc.).
 Example with ALL:
SELECT * FROM Products
WHERE Price > ALL (SELECT AVG(Price) FROM Products GROUP BY
Category);
This query retrieves all products where the Price is greater than the average price of
products in each category.
 Example with ANY or IN:
SELECT * FROM Orders
WHERE CustomerID = ANY (SELECT CustomerID FROM Customers WHERE
Country = 'USA');
This query retrieves all orders placed by customers from the USA.
7. IS NULL / IS NOT NULL
The IS NULL and IS NOT NULL operators are used to test for NULL values in columns.
 Example:
SELECT * FROM Employees
WHERE Email IS NULL;
This query retrieves all rows from the Employees table where the Email column is
NULL.
8. DISTINCT
The DISTINCT keyword is used in a SELECT statement to eliminate duplicate rows from the
result set.
 Example:
SELECT DISTINCT Department FROM Employees;
This query retrieves all unique Department values from the Employees table,
eliminating duplicates.
Example Usage in SQL Queries
Here are some scenarios demonstrating the use of these operators and keywords in SQL
queries:
-- Select customers whose name starts with 'A' or 'B'
SELECT * FROM Customers
WHERE CustomerName LIKE 'A%' OR CustomerName LIKE 'B%';

-- Select products with a price between $50 and $100


SELECT * FROM Products
WHERE Price BETWEEN 50 AND 100;

-- Select orders with an order total greater than any order total placed by
customer ID 123
SELECT * FROM Orders
WHERE OrderTotal > ANY (SELECT OrderTotal FROM Orders WHERE CustomerID =
123);

-- Select customers who have placed at least one order


SELECT * FROM Customers
WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID);

-- Select employees with a salary that is not in a specified list


SELECT * FROM Employees
WHERE Salary NOT IN (50000, 60000, 70000);

-- Select distinct countries from the customers table


SELECT DISTINCT Country FROM Customers;
Notes:
 These operators and keywords provide powerful capabilities for querying and
manipulating data in SQL.
 Understanding their usage and behavior is crucial for writing efficient and effective
SQL queries.
 Always consider performance implications, especially when using subqueries and
operations that involve large datasets.

You might also like