1. What is a Database?
A Database is a structured collection of data that is organized, stored, and managed so it can be easily accessed, updated, and managed.
Key Points:
- Stores information in a structured way (tables, rows, columns).
- Enables efficient data retrieval and manipulation.
- Used in almost every software application: websites, mobile apps, ERP systems, etc.
2. Types of Databases
| Type | Description | Examples |
|---|---|---|
| Relational Database (RDBMS) | Data is stored in tables with rows and columns, relationships between tables are maintained | MySQL, PostgreSQL, Oracle, SQL Server |
| NoSQL Database | Non-tabular, flexible schema, good for unstructured data | MongoDB, Cassandra, Redis |
| In-Memory Database | Stores data in memory for faster access | Redis, Memcached |
| Cloud Database | Hosted in the cloud, scalable | AWS RDS, Google Cloud SQL, Azure SQL Database |
3. What is SQL?
SQL (Structured Query Language) is a standard language used to communicate with relational databases.
Purpose:
- Retrieve data (
SELECT) - Insert data (
INSERT) - Update data (
UPDATE) - Delete data (
DELETE) - Create and modify database objects (
CREATE,ALTER,DROP)
4. Basic SQL Commands
A. Data Definition Language (DDL)
Used to create and modify database structure.
| Command | Description | Example |
|---|---|---|
CREATE DATABASE | Create a new database | CREATE DATABASE mydb; |
CREATE TABLE | Create a new table | CREATE TABLE users(id INT, name VARCHAR(50)); |
ALTER TABLE | Modify table structure | ALTER TABLE users ADD email VARCHAR(50); |
DROP TABLE | Delete a table | DROP TABLE users; |
B. Data Manipulation Language (DML)
Used to manipulate data in tables.
| Command | Description | Example |
|---|---|---|
INSERT INTO | Insert new records | INSERT INTO users(id, name) VALUES(1, 'John'); |
SELECT | Retrieve records | SELECT * FROM users; |
UPDATE | Update existing records | UPDATE users SET name='Mike' WHERE id=1; |
DELETE | Delete records | DELETE FROM users WHERE id=1; |
C. Data Control Language (DCL)
Used to control access to the database.
| Command | Description | Example |
|---|---|---|
GRANT | Give permissions | GRANT SELECT ON users TO 'testuser'; |
REVOKE | Remove permissions | REVOKE SELECT ON users FROM 'testuser'; |
D. Transaction Control Language (TCL)
Used to manage transactions.
| Command | Description |
|---|---|
COMMIT | Save all changes |
ROLLBACK | Undo changes |
SAVEPOINT | Set a point to rollback to |
5. Database Concepts
- Table – Collection of related data in rows and columns.
- Row / Record – Single entry in a table.
- Column / Field – Attribute of a record.
- Primary Key (PK) – Unique identifier for each record.
- Foreign Key (FK) – Field linking one table to another.
- Index – Improves search performance.
- Normalization – Organizing data to reduce redundancy.
- Relationships:
- One-to-One – One record in table A corresponds to one record in table B.
- One-to-Many – One record in table A corresponds to multiple records in table B.
- Many-to-Many – Many records in table A correspond to many in table B (via junction table).
6. SQL Joins
Used to combine data from multiple tables.
| Join Type | Description |
|---|---|
| INNER JOIN | Returns records with matching values in both tables |
| LEFT JOIN (or LEFT OUTER) | Returns all records from left table, matched records from right table |
| RIGHT JOIN (or RIGHT OUTER) | Returns all records from right table, matched records from left table |
| FULL OUTER JOIN | Returns all records when there is a match in one of the tables |
| CROSS JOIN | Returns Cartesian product (all combinations) |
7. Best Practices for SQL & Databases
- Always use primary keys for tables.
- Avoid redundant data; normalize tables.
- Use indexes to improve performance.
- Use transactions for critical operations.
- Write clear and optimized queries to reduce load.
- Backup databases regularly.
- Use descriptive names for tables and columns.
8. Summary
- Database stores structured information efficiently.
- SQL is used to create, read, update, and delete data in relational databases.
- Basic SQL commands include DDL, DML, DCL, and TCL.
- Key concepts include tables, keys, relationships, joins, and normalization.
- Good practices improve performance, scalability, and reliability.