DBMS
DBMS
There can only be one primary key in a table, but that primary key
can consist of one or more columns. When there are two or more
columns in the primary key it is called a composite key.
2. NO NULL values are present in the Primary key column. Hence there
is a Mandatory value in the column having the Primary key.
3. Only one primary key per table exists although the Primary key may
have multiple columns.
4. No new row can be inserted with the already existing primary key.
Syntax
There are two syntaxes to create/add primary key to a table:
Query
FirstName varchar(255),
Age int
);
Query
Output
Let's consider previous table, and create it without primary key this
time.
PersonID int,
Conclusion
This article will explore the fundamentals of INNER JOIN, its syntax,
practical examples, and the key differences between INNER
JOIN and other types of joins, such as OUTER JOIN.
The key feature of an INNER JOIN is that it filters out rows from
the result where there is no matching data in both tables.
Essentially, it returns a "subset" of the data where the condition is
satisfied.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Key Terms
professor table
Nam Sala
ID e ry
Roha 5700
1
n 0
Arya 4500
2
n 0
6000
3 Arpit
0
Hars 5000
4
h 0
5500
5 Tara
0
teacher Table
1 1 English
1 3 Physics
2 4 Chemistry
Mathematic
2 5
s
Query
Output
Roha 5700
1 1
n 0
6000
1 3 Arpit
0
Hars 5000
2 4
h 0
5500
2 5 Tara
0
Explanation:
INNER JOIN
Conclusion
The optimized SQL queries not only enhance the performance but
also contribute to cost savings by reducing resource consumption.
Let us see the various ways in which you can optimize SQL queries
for faster performance.
1. Use Indexes
Indexes act like internal guides for the database to locate specific
information quickly. Identify frequently used columns in WHERE
clauses, JOIN conditions and ORDER BY clauses then create indexes
on those columns. However, creating too many indexes can slow
down adding and updating data so use them strategically.
For Example:
This is one of the best optimization techniques that you must follow.
Running queries inside the loop will slow down the execution time to
a great extent. In most cases, you will be able to insert and update
data in bulk which is a far better approach as compared to queries
inside a loop.
Both Exist() and Count() are used to search whether the table has a
specific record or not. But in most cases Exist() is much more
effective than Count(). As Exist() will run till it finds the first
matching entry whereas Count() will keep on running and provide all
the matching records. Hence this practice of SQL query optimization
saves a lot of time and computation power. EXISTS stop as the
logical test proves to be true whereas COUNT(*) must count each
and every row, even after it has passed the test.
Cartesian products occur when every row from one table is joined
with every row from another table, resulting in a massive dataset.
Accidental Cartesian products can severely impact query
performance. Always double-check JOIN conditions to
avoid unintended Cartesian products. Make sure you're joining the
tables based on the specific relationship you want to explore.
For Example
9. Consider Denormalization
For Example:
Conclusion
Following these best practices for SQL query optimization, you can
ensure that your database queries run efficiently and deliver results
quickly. This will not only improve the performance of your
applications but also enhance the user experience by minimizing
delays. Therefore the key benefits of SQL query optimization are
improved performance, faster results, and a better user experience.
Normal
Condition/Description
Form
First
Normal - All attributes must be atomic (single-valued).-
Form No repeating groups or arrays allowed in a table.
(1NF)
Boyce-
Codd
- Must be in 3NF.- For every functional
Normal
dependency X → Y, X must be a super key.
Form
(BCNF)
Fourth
Normal - Must be in BCNF.- Must not contain any multi-
Form valued dependency (MVD).
(4NF)
Fifth
- Must be in 4NF.- Should not have any join
Normal
dependency that results in lossless
Form
decomposition.
(5NF)
Attributes of Indexing
Also
Scale-up Scale-out
Known As
Upgrade the
How It Add more servers
existing server
Works to the system
(RAM, CPU)
1 server → 3
8GB RAM → 16GB
Example servers working in
RAM
parallel
8. What is sharding
Sharding is the process of breaking a large database into smaller
parts called shards. Each shard holds a subset of the data and is
stored on a separate server.
Why Shard?
To handle large volumes of data
Improve performance
Reduce server load
Example:
User table:
o Shard 1: Users A–M
o Shard 2: Users N–Z
Each server manages fewer users → faster queries!
Note: It’s used in horizontal scaling.
9. Keys in DBMS
Keys are special fields used to uniquely identify rows in a table.
Types of Keys:
1. Primary Key – Main key to uniquely identify a row (cannot be
NULL).
2. Candidate Key – Fields that can qualify as a Primary Key.
3. Super Key – Any set of fields that can uniquely identify a row (may
include extra fields).
4. Alternate Key – Candidate keys not selected as the primary.
5. Foreign Key – A field in one table that refers to the primary key of
another.
6. Composite Key – Made up of two or more fields to identify a
record.
Example:
In Students table: Roll No = Primary Key
In Marks table: Roll No = Foreign Key (refers to Students)
Example:
Normalized Structure:
Denormalized:
Advantages:
Faster SELECT queries
Easier reporting
Disadvantages:
Redundant data
Definition:
Notation:
A→B
A: Determinant
B: Dependent attribute
Example:
RollNo → StudentName
FDs are used to identify candidate keys and form the basis for
normalization.
ER Diagram Symbols:
Rectangle → Entity
Ellipse → Attribute
Diamond → Relationship
Conflict Conditions:
Nodes: Transactions
Example:
o Must be in 1NF
o Must be in 2NF
o No transitive dependency
1. Lock-Based Protocols
o Ensures serializability
2. Timestamp-Based Protocols
Example:
Fetches data
Command: SELECT
Where Used?
Types of Subqueries:
Correlated Example:
WHERE EXISTS (
A JOIN allows you to combine rows from two or more tables based on a
related column between them.
Types of Joins:
1. INNER JOIN:
o Returns all rows from the left table and matched rows from the
right
o Returns all rows from the right table and matched rows from
the left
9. CROSS JOIN:
INNER JOIN:
Returns only the rows that have matching values in both tables.
FROM Orders
OUTER JOIN:
Returns all matching rows, plus unmatched rows from one or both
tables.
1. LEFT OUTER JOIN: Returns all rows from the left table, and
matched rows from the right. If no match, NULLs.
FROM Orders
2. RIGHT OUTER JOIN: Returns all rows from the right table, and
matched rows from the left. If no match, NULLs.
FROM Orders
RIGHT JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
3. FULL OUTER JOIN: Returns all rows when there is a match in one of
the tables. Where no match, NULLs are filled in.
FROM Orders
Slower (row-by-row
Speed Faster (resets entire table)
deletion)
Transaction
Yes, can ROLLBACK No, cannot be rolled back
control
Called
Example:
T1 and T2 both can read the same row using shared locks
Example:
Exclusive
Shared Lock
Lock
Exclusiv
❌ Not Allowed ❌ Not Allowed
e
These locks are used in protocols like Two-Phase Locking (2PL) to
ensure serializability in concurrent transactions.
Let me know if you'd like more practice queries, visuals for join
operations, or revision notes!
Definition
File System:
A file system is a method of storing and organizing files on a storage
device (like HDD or SSD). It manages how data is named, stored, and
retrieved. It organizes data into files and directories.
DBMS (Database
Feature File System
Management System)
Organizes data in
Organizes data as
Structure structured tables with
files in directories
relationships
High redundancy
Data Minimal redundancy using
(duplicate data may
Redundancy normalization
exist)
access
Summary:
Conclusion:
The File System is basic, lightweight, and best for small, standalone
applications. In contrast, DBMS provides comprehensive, scalable, and
secure data management for modern applications requiring
consistency and concurrent access.