If there’s a match between A and B, it combines them.
If there’s no match, you still get the row — with NULL in the missing side.
Summary:
Situation
FULL OUTER JOIN behavior
Row matches in both A and B
Combine them.
Row in A, but no match in B
Show A row + NULL for B.
Row in B, but no match in A
Show B row + NULL for A.
Example
Customers
customer_id
name
1
Alice
2
Bob
3
Charlie
Orders
order_id
customer_id
product
101
1
Laptop
102
2
Smartphone
103
4
Tablet
Query:
SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.product
FROM Customers
FULL OUTER JOIN Orders
ON Customers.customer_id = Orders.customer_id;
Result:
customer_id
name
order_id
product
1
Alice
101
Laptop
2
Bob
102
Smartphone
3
Charlie
NULL
NULL
NULL
NULL
103
Tablet
✅ What’s happening:
Alice and Bob match an order — perfect, rows are merged.
Charlie has no orders — so we see his row with NULL for order_id and product.
Order 103 is for customer_id = 4, but no such customer — so customer_id and name are NULL on the left side.
Why Use FULL OUTER JOIN?
When you want to see everything: all customers, all orders — even if there’s no match.
Useful for reporting, audits, or finding mismatches (like customers without orders, or orders without customers).
For example:
Customers who haven’t ordered anything yet.
Orders placed but no matching customer exists (e.g., data entry errors).
FULL OUTER JOIN = LEFT JOIN + RIGHT JOIN
You could think of it as:
SELECT * FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id
UNION
SELECT * FROM Customers
RIGHT JOIN Orders ON Customers.customer_id = Orders.customer_id;
But FULL OUTER JOIN does this in one step.
Limitations
Not supported by all databases natively (e.g., MySQL doesn’t support FULL OUTER JOIN directly — you have to simulate it with UNION of LEFT JOIN and RIGHT JOIN).
Denormalization is the process of intentionally adding redundancy into a database — basically undoing some of the normalization — in order to improve read performance.
Key Idea:
In normalization, we split data into multiple related tables to reduce redundancy.
In denormalization, we combine tables or add redundant columns to make reads faster (at the cost of some redundancy).
Why Denormalize?
Performance:
Complex queries in a fully normalized database might require multiple JOIN operations.
Joins are expensive — they cost time and resources.
Denormalization can speed up queries by reducing or eliminating joins.
Simpler Queries:
Fewer joins mean easier, faster queries.
Precomputed Data:
Sometimes, denormalization involves storing computed values like totals, counts, or derived data to avoid recalculating them.
Example
Normalized Tables (good design, normalized to 3NF):
Customers
customer_id
customer_name
1
Alice
2
Bob
Orders
order_id
customer_id
order_date
101
1
2025-06-01
102
2
2025-06-02
To get the customer’s name with the order:
SELECT o.order_id, c.customer_name, o.order_date
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id;
Denormalized Table:
order_id
customer_id
customer_name
order_date
101
1
Alice
2025-06-01
102
2
Bob
2025-06-02
Now:
customer_name is stored redundantly in the Orders table.
No JOIN needed to fetch the customer name!
But:
If Bob changes his name, you have to update it everywhere Bob’s name appears — risk of inconsistency.
Advantages vs Disadvantages
Advantages
Disadvantages
Faster reads (less joins).
Redundancy — same data in many places.
Simpler queries (fewer joins to write).
Harder to update — more maintenance.
Precomputed values can improve speed.
Risk of inconsistencies.
Better for reporting, analytics systems.
More storage space used.
In Simple Words:
Normalization = Break apart to avoid redundancy. Good for updates and data consistency.
Denormalization = Copy data around to make reads faster. Good for performance and speed.
In Simple Words:
Normalization = Break apart to avoid redundancy. Good for updates and data consistency.
Denormalization = Copy data around to make reads faster. Good for performance and speed.
Real-World Analogy
Normalized: You have one file cabinet for customers, another for orders — to find a customer’s order, you cross-reference both.
Denormalized: You make a folder for each order and also write the customer name on each one — easier to find, but if the customer changes their name, you have to update every folder manually!
Posted inБез рубрики|Comments Off on Database.What is Denormalization?
A partial dependency happens when a column (like student_name) depends on part of a composite key (like student_id), not the whole key (student_id, course).
Let’s See the Table Again
Imagine this table:
student_id
course
student_name
1
Math
Alice
1
History
Alice
2
Math
Bob
Suppose:
Primary Key = (student_id, course) → both columns together uniquely identify a row.
This means bothstudent_idandcourse are needed to tell one row apart from another.
What depends on what?
student_name — it depends only on student_id.
Wherever student_id = 1, the name is always “Alice”, no matter what course.
It has nothing to do with which course the student takes.
But:
In a properly normalized table, all non-key columns should depend on the whole key — not just a part of it.
Why is this bad?
Redundancy: “Alice” is repeated for every course Alice takes.
Update problem: If Alice changes her name, you must update it in every row where student_id = 1.
Data inconsistency: You could accidentally have “Alice” for Math and “Alise” for History — two different names for the same student!
Real-world Analogy
Think of it like this:
Suppose you have a rental system where the key is (PersonID, CarID) because one person can rent multiple cars.
But you store the person’s name with each rental.
The name depends only on the person — not on which car they rent.
You shouldn’t repeat the name for each rental; you should store it once in a Person table!
Correct Fix — Split the Table
Students (student_id → student_name):
student_id
student_name
1
Alice
2
Bob
Enrollments (student_id + course):
student_id
course
1
Math
1
History
2
Math
Now:
student_name depends only on student_id (good — and we don’t repeat it).
course depends on both student_id and course (good — this pair is unique).
Key Point
In 2NF, every non-key column must depend on the whole composite key — not just part of it.
If you can say:
“This column only needs student_id to be known, not the full (student_id, course)” — then you have a partial dependency, and you are NOT in 2NF.
Posted inБез рубрики|Comments Off on Database.Beginner.2NF.Details
Normalization is the process of organizing data in a database to reduce redundancy (duplicate data) and improve data integrity.
It involves breaking a big, messy table into smaller, more focused tables and linking them through relationships (usually using primary and foreign keys).
Key Goals of Normalization:
Eliminate redundant data — avoid storing the same data in multiple places.
Ensure data dependencies make sense — data should be stored logically.
Protect against anomalies:
Insertion anomaly: Trouble inserting data without other required data.
Update anomaly: Changing data in one place but not another, causing inconsistency.
Deletion anomaly: Losing important data because of deleting other data.
Why is Normalization Important?
Reduces data redundancy:
Saves storage space.
Keeps data clean and consistent.
Improves data integrity:
Avoids conflicting information in the database.
Ensures changes are propagated correctly.
Makes databases easier to maintain:
Easier to add, update, or delete data.
Fewer places to update if something changes.
Optimizes query performance(sometimes):
Smaller tables mean faster queries.
But — sometimes over-normalization can cause too many joins and slow things down (so we balance it carefully).
Example: Why Normalize?
Imagine a table like this:
student_id
student_name
course_1
course_2
1
Alice
Math
History
2
Bob
Math
NULL
Problems:
What if a student takes 3 courses? We’d have to add more columns.
If you update Math to Advanced Math, you have to update multiple places.
If you delete Bob, you lose record of Math.
After Normalization:
Students Table:
student_id
student_name
1
Alice
2
Bob
Courses Table:
course_id
course_name
1
Math
2
History
Enrollments Table (relationship table):
student_id
course_id
1
1
1
2
2
1
Now:
Courses are not duplicated.
We can add more courses easily.
No update anomalies — change Math name once in Courses.
In Short:
Without Normalization
With Normalization
Redundant data
Reduced redundancy
Update anomalies
Easier updates
Hard to scale
Easier to add new data
Risk of inconsistencies
Improved data integrity
DELETION ANOMALY
Let’s go back to this non-normalized table:
student_id
student_name
course_1
course_2
1
Alice
Math
History
2
Bob
Math
NULL
Now imagine you delete Bob:
DELETE FROM table WHERE student_id = 2;
Now the table looks like:
student_id
student_name
course_1
course_2
1
Alice
Math
History
➡️ Problem:
Suppose Bob was the only student who had Math as their course (let’s imagine Alice drops Math later).
When you delete Bob, you are not just deleting the student — you are also deleting the only place where “Math” was stored in the table.
You’ve lost track of the fact that the course “Math” even existed because courses and students are all stored together in one row.
This is called a deletion anomaly.
With Normalization
When you separate Students and Courses into different tables:
Courses Table:
course_id
course_name
1
Math
2
History
Now, even if you delete Bob from the Students table:
DELETE FROM Students WHERE student_id = 2;
The Courses table still keeps the record that “Math” is a valid course — no data loss!
In simple terms:
Without normalization:
Some important data (like the course “Math”) exists only inside student rows.
Deleting the student can accidentally delete information about the course too.
With normalization:
Students and courses are separate.
Deleting a student deletes only the student, not the course information.
1. Insertion Anomaly
An insertion anomaly happens when the database design prevents you from inserting data unless you have extra, unnecessary information.
Example without normalization:
student_id
student_name
course_name
1
Alice
Math
2
Bob
History
Problem:
Suppose you want to add a new coursePhysics that no students have enrolled in yet.
How would you do it?
You can’t insert:
INSERT INTO table (student_id, student_name, course_name)
VALUES (NULL, NULL, 'Physics');
You can’t insert a course without a student.
The database forces you to either put fake student data or leave it empty, which can break integrity.
With normalization:
Separate the tables:
Courses Table:
course_id
course_name
1
Math
2
History
Now you can easily:
INSERT INTO Courses (course_id, course_name) VALUES (3, 'Physics');
✅ No need for any student — you can add courses freely.
2. Update Anomaly
An update anomaly happens when the same data is stored in multiple places and you have to update it in all of them manually — or risk inconsistency.
Example without normalization:
student_id
student_name
course_name
1
Alice
Math
2
Bob
Math
3
Charlie
Math
Suppose the course “Math” is now renamed to “Advanced Mathematics.”
Problem:
You have to run:
UPDATE table SET course_name = 'Advanced Mathematics' WHERE course_name = 'Math';
But what if:
You forget to update one row?
You make a mistake in your WHERE condition?
Result: some students show “Math,” others show “Advanced Mathematics” — data inconsistency!
With normalization:
Courses Table:
course_id
course_name
1
Math
Enrollments Table:
student_id
course_id
1
1
2
1
3
1
Now, to rename the course:
UPDATE Courses SET course_name = 'Advanced Mathematics' WHERE course_id = 1;
✅ Update one place, and all students automatically point to the updated course name.
No inconsistency.
You forget to update one row?
Imagine you have this non-normalized table:
student_id
student_name
course_name
1
Alice
Math
2
Bob
Math
3
Charlie
Math
Now, suppose you manually update:
UPDATE table SET course_name = 'Advanced Mathematics' WHERE student_id = 1;
You updated only Alice‘s row.
You forgot to update Bob and Charlie.
Result:
student_id
student_name
course_name
1
Alice
Advanced Mathematics
2
Bob
Math
3
Charlie
Math
Now your database is inconsistent:
Alice has the new course name.
Bob and Charlie still have the old name.
You make a mistake in your WHERE condition?
Let’s say you want to update all rows where the course is “Math”:
UPDATE table SET course_name = 'Advanced Mathematics' WHERE course_name = 'Math';
But what if you accidentally write:
UPDATE table SET course_name = 'Advanced Mathematics' WHERE course_name = 'Mat'; -- typo!
Now the database finds zero rows matching 'Mat', so nothing changes.
You thought the update worked, but actually no rows got updated.
Or worse, what if you wrote:
UPDATE table SET course_name = 'Advanced Mathematics' WHERE student_id = 2;
UPDATE table SET course_name = 'Advanced Mathematics' WHERE student_id = 2;
Only Bob’s course name would be updated — Alice and Charlie stay unchanged.
Why is this bad?
Human error: You have to trust that every update touches exactly the right rows — but humans make mistakes.
Inconsistency: Different rows now have different values for what should be the same course.
How Normalization helps
If you had a separate Courses table:
course_id
course_name
1
Math
and students just point to course_id, you would do:
UPDATE Courses SET course_name = 'Advanced Mathematics' WHERE course_id = 1;
✅ Only one row to update. ✅ No chance to forget or miss any student. ✅ All students who point to course_id = 1 automatically see the updated course name.
Summary
Without Normalization
Risk
With Normalization
Safe!
Duplicate course names in multiple rows
You can forget to update some rows
Single source of truth
Only one place to update
In Short:
Anomaly
Problem without normalization
Solved by normalization
Insertion
Can’t insert a course without a student.
Separate course table — insert freely.
Update
Must update the same value in many rows, risking inconsistency.
Update only once in a separate table.
Posted inБез рубрики|Comments Off on Database.Beginner.What is normalization? Why is it important?
When you set up a foreign key with ON DELETE CASCADE, it means:
If you delete a row in the parent table (the table with the primary key), all related rows in the child table (the table with the foreign key) are automatically deleted.
This keeps the database consistent and avoids “orphan” records (child records without a parent).
A foreign key is a column (or a set of columns) in a table that creates a link between data in two tables. It acts as a reference to a primary key in another table.
Key points about foreign keys:
Referential Integrity: The foreign key ensures that the value in the column must match a value in the referenced table’s primary key (or a unique key).
Relationships: It’s used to define relationships between tables (e.g., one-to-many, many-to-one).
Validation: You cannot insert a value into a foreign key column if that value doesn’t exist in the referenced table.
Cascading: You can define actions like ON DELETE CASCADE or ON UPDATE CASCADE to automatically delete or update dependent rows.