Showing posts with label Learn SQL. Show all posts
Showing posts with label Learn SQL. Show all posts

CRUD Operations in SQL vs NoSQL Explained with Simple Examples

๐Ÿ”ท Part 6: CRUD Operations in SQL vs NoSQL – A Beginner's Guide


This post will:

  • ✅ Compare how to CreateReadUpdate, and Delete data in both systems

  • ✅ Use simple, realistic examples

  • ✅ Help beginners understand key syntax differences


๐Ÿ“ Introduction

In any application or system that works with data, you need to perform four basic operations:

  • Create new data

  • Read existing data

  • Update current data

  • Delete data you no longer need

These are called CRUD operations — and whether you're using SQL or NoSQL, they form the core of working with databases.

Let’s explore these operations in both SQL (like MySQL/PostgreSQL) and NoSQL (like MongoDB) with clear, side-by-side examples.


๐Ÿ”ธ Assumed Data Structure

We’ll use a simple students table/collection with:

  • StudentID

  • Name

  • Class

  • Marks (embedded in NoSQL)


๐Ÿ“Œ 1. CREATE – Inserting Data

✅ SQL (MySQL/PostgreSQL)

INSERT INTO Students (StudentID, Name, Class)
VALUES (1, 'Aisha', '10A');

✅ NoSQL (MongoDB)

db.students.insertOne({
  student_id: 1,
  name: "Aisha",
  class: "10A",
  marks: [
    { subject: "Math", score: 85 }
  ]
});

๐Ÿ“Œ 2. READ – Retrieving Data

✅ SQL

SELECT * FROM Students WHERE StudentID = 1;

✅ NoSQL

db.students.findOne({ student_id: 1 });

๐Ÿ“Œ 3. UPDATE – Changing Data

✅ SQL

UPDATE Students
SET Class = '10B'
WHERE StudentID = 1;

✅ NoSQL

db.students.updateOne(
  { student_id: 1 },
  { $set: { class: "10B" } }
);

๐Ÿ“Œ 4. DELETE – Removing Data

✅ SQL

DELETE FROM Students
WHERE StudentID = 1;

✅ NoSQL

db.students.deleteOne({ student_id: 1 });

๐Ÿ“Š Quick Comparison Table

Operation SQL Syntax MongoDB (NoSQL) Syntax
Create INSERT INTO ... VALUES (...) insertOne({ ... })
Read SELECT * FROM ... WHERE ... findOne({ ... })
Update UPDATE ... SET ... WHERE ... updateOne({ ... }, { $set: { ... } })
Delete DELETE FROM ... WHERE ... deleteOne({ ... })

๐Ÿง  Summary

SQL NoSQL (MongoDB)
Structured (tables/rows) Flexible (documents/JSON)
Uses SQL language Uses JavaScript-like syntax
Schema-based Schema-less
Great for complex queries Great for rapid, dynamic data

✅ What’s Next?

In Part 7, we’ll explore Normalization vs Denormalization — how SQL and NoSQL structure data differently for performance and flexibility.


Understanding Primary and Foreign Keys in Databases (With Examples for Beginners)

 

← Back to Home

๐Ÿ”ท Part 4: Primary Keys and Foreign Keys – Building Relationships in Databases


๐Ÿ“ Introduction

So far, we’ve learned how data is stored in tables and how we use SQL to interact with it. But what happens when your database has more than one table?

That’s where relationships come in — and they’re built using keys.

Keys help connect tables, maintain data integrity, and allow complex queries across multiple data sets. Today, we’ll explore Primary Keys and Foreign Keys, the foundation of relational database design.


๐Ÿ”‘ What is a Primary Key?

A Primary Key is a unique identifier for each record in a table. No two rows can have the same primary key, and it can’t be left empty (NULL).

๐Ÿ” Example – Students Table:

| StudentID | Name  | Course     |
|-----------|-------|------------|
| 1         | Aisha | Math       |
| 2         | Ravi  | Science    |
| 3         | Sara  | English    |

Here, StudentID is the Primary Key — each student has a unique ID.

๐Ÿ“˜ Think of it like a roll number in a classroom — no two students have the same roll number.


๐Ÿ”— What is a Foreign Key?

A Foreign Key is a column in one table that refers to the primary key in another table. It creates a link between two related tables.


๐Ÿงฉ Example: Students & Results Tables

๐Ÿงพ Students Table

| StudentID | Name  |
|-----------|-------|
| 1         | Aisha |
| 2         | Ravi  |

๐Ÿงพ Results Table

| ResultID | StudentID | Subject  | Marks |
|----------|-----------|----------|-------|
| 101      | 1         | Math     | 85    |
| 102      | 2         | Science  | 90    |

In this case:

  • StudentID is the Primary Key in the Students table.

  • StudentID in the Results table is a Foreign Key — it refers to the Students table.

๐Ÿ’ก This relationship ensures that every result belongs to a valid student.


๐Ÿ” Why Use Keys and Relationships?

  • Data Integrity: Prevents orphaned or mismatched records

  • Less Redundancy: No need to repeat data across tables

  • Scalability: Makes your database easier to maintain as it grows

  • Real-Life Modeling: Mimics real-world relationships (students have results, customers place orders, etc.)


๐Ÿ”ง SQL Example: Defining Primary and Foreign Keys

-- Create Students Table
CREATE TABLE Students (
  StudentID INT PRIMARY KEY,
  Name VARCHAR(100)
);

-- Create Results Table
CREATE TABLE Results (
  ResultID INT PRIMARY KEY,
  StudentID INT,
  Subject VARCHAR(50),
  Marks INT,
  FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);

๐Ÿ“š Real-Life Analogy

Imagine:

  • Primary Key = National ID Number

  • Foreign Key = Form asking for your National ID

You can’t submit the form unless your ID is valid — just like foreign keys rely on real, matching primary keys.


๐Ÿง  Recap

  • Primary Key: Uniquely identifies a row in a table (e.g., StudentID)

  • Foreign Key: Connects a row to another table using a reference

  • These keys help create relationships between tables and ensure the accuracy and reliability of your data.


✅ What’s Next?

In Part 5, we’ll explore NoSQL databases — a modern alternative to relational databases that works better for unstructured data, large-scale applications, and real-time needs.



Introduction to SQL: Beginner’s Guide to Database Language (With Simple Examples)

← Back to Home

Part 3: Introduction to SQL – Learn the Language of Databases


๐Ÿ“ Introduction

Databases store data — but how do we talk to them? That’s where SQL (Structured Query Language) comes in. SQL is the language we use to interact with relational databases: we ask questions, make changes, and organize information — all using SQL commands.

Whether you want to retrieve customer data from an online store or insert marks into a student database, SQL is your go-to tool.


๐Ÿ”น What is SQL?

SQL (Structured Query Language) is a programming language used to create, read, update, and delete data in a relational database. These four actions are commonly referred to as CRUD operations.

SQL works across many relational databases like MySQL, PostgreSQL, SQLite, Oracle, and SQL Server.


๐Ÿง  Think of SQL as a Conversation

Imagine a database is like a library.

  • If you want to find a book, you say: "Show me all the books by J.K. Rowling."

  • If you want to add a book, you say: "Add this new book to the shelf."

  • If you want to update a book’s details, you give new information.

  • If you want to remove a book, you say: "Take this book off the shelf."

SQL is how we say those things to a database — in a structured, computer-friendly way.


๐Ÿ› ️ Basic SQL Commands (With Examples)

Let’s explore the most common SQL commands using a Students table example:

๐Ÿ“Œ 1. SELECT – Read Data

SELECT * FROM Students;

Retrieves all records from the Students table.

SELECT Name, Marks FROM Students WHERE Marks > 80;

Shows names and marks of students who scored above 80.


๐Ÿ“Œ 2. INSERT – Add Data

INSERT INTO Students (Name, Course, Marks)
VALUES ('Nina', 'Science', 88);

Adds a new student named Nina with a Science course and 88 marks.


๐Ÿ“Œ 3. UPDATE – Modify Data

UPDATE Students
SET Marks = 95
WHERE Name = 'Ravi';

Updates Ravi’s marks to 95.


๐Ÿ“Œ 4. DELETE – Remove Data

DELETE FROM Students WHERE Name = 'Sara';

Deletes the record for Sara.


๐Ÿ“‹ Example Table for Context

| StudentID | Name  | Course     | Marks |
|-----------|-------|------------|-------|
| 1         | Aisha | Math       | 85    |
| 2         | Ravi  | Science    | 90    |
| 3         | Sara  | English    | 78    |

These SQL commands let you manage data in this table easily and efficiently.


๐Ÿ’ก Pro Tip: Case Sensitivity

SQL keywords (like SELECT, FROM, WHERE) are not case-sensitive, but it's good practice to write them in uppercase for readability.


Recap

  • SQL is the language used to interact with relational databases.

  • The four key operations are: SELECT, INSERT, UPDATE, and DELETE.

  • You can retrieve, add, edit, and remove data using easy-to-understand commands.

  • SQL works across most relational databases like MySQL, PostgreSQL, and SQLite.


✅ What’s Next?

In Part 4, we’ll explore Keys and Relationships — the magic behind how multiple tables in a database talk to each other. You’ll learn about Primary Keys, Foreign Keys, and how to build logical connections in your data.



Relational Databases for Beginners: Simple Guide with Examples

 

← Back to Home

Part 2: Understanding Relational Databases and Tables


Introduction

In our first post, we learned what a database is and why it is essential in today’s digital world. Now, let us take a closer look at relational databases, the most common type used in everything from websites to banking systems.

Relational databases store data in a structured format using tables — a simple but powerful way to organize and retrieve information efficiently.


๐Ÿ”น What is a Relational Database?

A Relational Database is a type of database that organizes data into tables. Each table consists of rows (records) and columns (fields), and every row is uniquely identified — often by a Primary Key.

This structure is called “relational” because data in different tables can be related through keys.


๐Ÿ”น Tables, Rows, and Columns Explained

Let’s break down the basic components:

Table

A table represents a specific entity — like Students, Products, or Employees.

Row (Record)

Each row represents one item or entry in the table.

Column (Field)

Each column holds a specific type of data, like name, age, or email.


๐Ÿ“Š Example: A Simple Students Table

| StudentID | Name     | Course      | Marks |
|-----------|----------|-------------|-------|
| 1         | Aisha    | Mathematics | 85    |
| 2         | Ravi     | Science     | 90    |
| 3         | Sara     | English     | 78    |
  • StudentID is a Primary Key — a unique value for each student.

  • Name, Course, and Marks are fields that store the student’s information.

๐ŸŽ“ Real-Life Analogy: Think of a table as an Excel sheet — each row is a student, and each column is a category like name or marks.


๐Ÿ”น Why Use Tables?

  • Organization: Easy to categorize and search data

  • Relationships: Link data between multiple tables (e.g., a Students table and a Courses table)

  • Data Integrity: Reduces errors and redundancy

  • Performance: Optimized for quick queries and large datasets


๐Ÿ”— What Makes It “Relational”?

The real power of relational databases comes from their ability to relate data across multiple tables using keys:

  • Primary Key: A unique identifier for a row (like StudentID)

  • Foreign Key: A column in another table that refers to a primary key

We shall explore this more in Part 4: Keys and Relationships in Databases.


๐Ÿง  Quick Recap

  • A Relational Database stores data in structured tables.

  • Each row = a record, and each column = a field.

  • Tables can relate to one another using keys.

  • This format makes querying, updating, and organizing data efficient and reliable.


✅ What’s Next?

In Part 3, we’ll introduce SQL (Structured Query Language) — the language used to interact with relational databases. You’ll learn how to write simple SQL commands to create, read, and update data.



Featured Post

CRUD Operations in SQL vs NoSQL Explained with Simple Examples

๐Ÿ”ท Part 6: CRUD Operations in SQL vs NoSQL – A Beginner's Guide This post will: ✅ Compare how to  Create ,  Read ,  Update , and  Del...

Popular Posts