SQL & Database Basics
-
Learn the fundamentals of SQL and relational databases.
Database Concepts
What is a Database?
A Database is an organized collection of data stored electronically.
It helps to:
Store data
Manage data
Retrieve data
Update data
What is DBMS?
DBMS (Database Management System) is software used to manage databases.
Examples:
MySQL
PostgreSQL
MongoDB
Oracle Database
Types of Databases
Relational Database (RDBMS)
Data stored in tables
Uses SQL
Example: MySQL
NoSQL Database
Flexible structure
Stores JSON-like data
Example: MongoDB
Tables
A Table is used to store data in rows and columns.
Structure:
Columns → Attributes (ID, Name, Subject)
Rows → Records
Create Table Example:
Creating a Students Table in SQL
This SQL query creates a table named students with columns for id, name, subject, and marks to store student information and their marks.
CREATE TABLE students (
id INT,
name VARCHAR(50),
subject VARCHAR(50),
marks INT
);
- Insert Data:
Inserting Data into Students Table
This SQL query inserts a new record into the students table with student ID 1, name Rahul, subject Math, and marks 85.
INSERT INTO students VALUES (1, 'Rahul', 'Math', 85);
Primary Key
What is Primary Key?
A Primary Key is a column that:
Uniquely identifies each record
Cannot be NULL
Cannot have duplicate values
Example:
Primary Key in SQL
This example explains how to define a Primary Key in a SQL table. A Primary Key uniquely identifies each record and does not allow NULL or duplicate values.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
subject VARCHAR(50),
marks INT
);
Here:
id is Primary Key
No two students can have same ID
Why Primary Key is Important?
✔ Prevents duplicate records
✔ Maintains data integrity
✔ Improves query performance
✔ Helps create relationships between tablesReal-World Example
In Banking System:
Account_Number is Primary Key
Each account must be unique.