A database is an organized collection of data that can be easily accessed, managed, and updated.
In SQL terms, a database stores multiple tables, along with relationships, constraints, and other objects.
Example:
Database → SchoolDB
Tables inside → Students, Teachers, Courses
Although SQL is mostly associated with relational databases, there are multiple types:
| Type | Description | Examples |
|---|---|---|
| Relational (RDBMS) | Stores data in tables with rows & columns | MySQL, PostgreSQL, Oracle |
| NoSQL | Stores unstructured/semi-structured data | MongoDB, Cassandra |
| Distributed | Data stored across multiple machines | Amazon Aurora, Google Spanner |
| Data Warehouse | Optimized for analytics | Snowflake, Redshift |
RDBMS (Relational Database Management System) is the software used to manage SQL databases.
Key features:
Tables with rows (records) and columns (fields)
Primary keys to uniquely identify rows
Foreign keys to link related tables
Data integrity rules
-- Create a new database
CREATE DATABASE SchoolDB;
-- Use a database
USE SchoolDB;
Caution: This removes the database permanently.
DROP DATABASE SchoolDB;
SHOW DATABASES;
Export:
mysqldump -u root -p SchoolDB > backup.sql
Import:
mysql -u root -p SchoolDB < backup.sql
Inside a database, you can have:
Tables – Store data
Views – Virtual tables from queries
Stored Procedures – Predefined SQL routines
Triggers – Automatic actions when events happen
Indexes – Speed up searches
CREATE DATABASE SchoolDB;
USE SchoolDB;
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Grade VARCHAR(5)
);
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
TeacherID INT
);
CREATE TABLE Teachers (
TeacherID INT PRIMARY KEY,
TeacherName VARCHAR(100)
);
Always name databases and tables clearly
Use normalization to avoid redundant data
Create indexes for faster queries
Regularly backup databases
Use transactions for safe updates