0% found this document useful (0 votes)
7 views17 pages

DBMS Detailed Tutorial

Database management system in simple language

Uploaded by

pinalbhagia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

DBMS Detailed Tutorial

Database management system in simple language

Uploaded by

pinalbhagia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DBMS Concepts & SQL - Complete Tutor-Style Guide

A detailed, beginner-friendly guide with examples and diagrams. Designed to be a one-stop resource for
Database Management System concepts, SQL syntax, and practical tasks.

Contents:
- 1. Why use a DBMS?
- 2. Levels of Abstraction (diagram)
- 3. Schema vs Instance
- 4. Database Architecture & Storage Manager
- 5. ACID properties (diagram & examples)
- 6. Transactions and Concurrency (examples)
- 7. SQL: DDL and DML (examples)
- 8. Joins with diagrams and examples
- 9. Aggregates, GROUP BY, HAVING
- 10. Constraints, Keys and Indexes
- 11. Normalization with examples
- 12. Practical exercises and solutions
- 13. Quick SQL cheat sheet
- 14. Glossary
1. Why use a DBMS?
1) Why use a DBMS?

- Problems with simple file systems:


* Data redundancy and inconsistency (same data repeated in multiple files).
* Difficulty in sharing and accessing data across applications.
* Integrity problems and weak security.

Example:
- If student name appears in attendance.txt and marks.txt and is updated in
one file but not the other, the data becomes inconsistent.
How DBMS helps:
- Centralized data storage, controlled access, built-in integrity constraints
and query capabilities.
2. Levels of Abstraction
2) Levels of Abstraction

- Physical level: How data is actually stored on disk (blocks, files).


- Logical level: The schema or structure: tables, columns, relationships.
- View level: The part of the database seen by a user or application (may hide
columns such as salary).

See the diagram on the next page for a visual representation of the three
levels.
3. Schema vs Instance
3) Schema vs Instance

- Schema: The structure or blueprint of the database. Tables and column types.
This is stable.
- Instance: The actual data stored at any moment in time. This changes
frequently.

Example: For a bank system, schema defines Customer(CustomerID, Name, Address)


and Account(AccountNo, Balance). The instance is the current rows in these
tables.
4. Database Architecture & Storage Manager
4) Database System Architecture (brief)

- Major components: Query processor, Storage manager, Transaction manager,


Authorization manager.
- Storage manager handles: allocating disk space, caching frequently used data
in memory, and interacting with the file system or device manager.

Practical note:
- When you issue a SQL query, the query processor parses and optimizes it,
then the storage manager reads required pages from disk (or cache) and returns
results.
5. ACID Properties
5) ACID Properties (Atomicity, Consistency, Isolation, Durability)

- Atomicity: A transaction is all-or-nothing. If one part fails, the whole


transaction is rolled back.
Example: Transferring 100 from Account A to Account B:
BEGIN TRANSACTION;
UPDATE Account SET Balance = Balance - 100 WHERE AccountNo = 'A123';
UPDATE Account SET Balance = Balance + 100 WHERE AccountNo = 'B456';
COMMIT;
If the second update fails, the first must be undone.

- Consistency: Each transaction brings the database from one valid state to
another (all constraints satisfied).
Example: Not allowing a negative balance if a CHECK constraint forbids it.

- Isolation: Concurrent transactions do not interfere with each other. The


effect should be as if transactions ran serially.
Example: Two withdrawals from the same account should not cause a lost update.

- Durability: Once a transaction commits, its changes persist even after a


failure.

See the ACID diagram on the next page.


6. Transactions and Concurrency
6) Transactions and Concurrency

Common concurrency anomalies:


- Lost update: T1 reads, T2 reads, T1 writes, T2 writes overwriting T1's update.
- Dirty read: T1 writes but not committed yet, T2 reads the uncommitted data.
- Non-repeatable read: T1 reads a row, T2 updates it and commits, T1 reads again
and sees a different value.
- Phantom read: T1 reads a set of rows that satisfy a condition, T2 inserts a
new row that T1's later read will see.

Isolation levels (brief): READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ,


SERIALIZABLE.
7. SQL: DDL and DML (Examples)
7) SQL: DDL and DML - Practical examples

- Create a database and table:


CREATE DATABASE Company;
USE Company;

CREATE TABLE Product(


Pname VARCHAR(50) PRIMARY KEY,
Price FLOAT NOT NULL,
Category VARCHAR(30) CHECK (Category IN
('Gadgets','Photography','Household')),
Manufacturer VARCHAR(50)
);

- Insert rows:
INSERT INTO Product VALUES ('Gizmo', 19.99, 'Gadgets', 'GizmoWorks');
INSERT INTO Product VALUES ('SingleTouch', 149.99, 'Photography', 'Canon');

- Simple select:
SELECT * FROM Product;

- Conditional select:
SELECT Pname, Price FROM Product WHERE Price > 100;

- Using LIKE for pattern matching:


SELECT * FROM Product WHERE Pname LIKE 'P%'; -- names that start with P
8. Joins (with diagram)
8) Joins - concepts and examples

- INNER JOIN: Returns rows when there is a match in both tables.


Example: Find customers and their accounts:
SELECT c.Name, a.AccountNo, a.Balance
FROM Customer c
INNER JOIN Depositor d ON c.CustomerID = d.CustomerID
INNER JOIN Account a ON d.AccountNo = a.AccountNo;

- LEFT JOIN: All rows from left table plus matched rows from right. If no match,
right columns are NULL.
Example: List all customers and their accounts (if any):
SELECT c.Name, a.AccountNo
FROM Customer c
LEFT JOIN Depositor d ON c.CustomerID = d.CustomerID
LEFT JOIN Account a ON d.AccountNo = a.AccountNo;

See the Venn-style diagram on the next page.


9. Aggregation: GROUP BY and HAVING
9) Aggregates, GROUP BY, and HAVING

- Aggregate functions: SUM, AVG, COUNT, MAX, MIN.


Example: Total price of all products:
SELECT SUM(Price) FROM Product;

- GROUP BY: Use to aggregate by groups. Example: Average price per category:
SELECT Category, AVG(Price) FROM Product GROUP BY Category;

- HAVING: Filter groups produced by GROUP BY. Example: Categories with average
price > 50:
SELECT Category, AVG(Price) as AvgP FROM Product
GROUP BY Category
HAVING AVG(Price) > 50;
10. Constraints, Keys & Indexes
10) Constraints, Keys, Indexes

- PRIMARY KEY: Uniquely identifies a row. Implies NOT NULL and UNIQUE.
- FOREIGN KEY: Enforces referential integrity between tables.
- UNIQUE: Column values must be unique across rows.
- NOT NULL: Disallow null values in a column.
- CHECK: Custom condition enforced on column values.
- DEFAULT: Default value used when none provided.

- Indexes: Help queries run faster on columns used in WHERE, JOIN or ORDER BY.
Example to create an index:
CREATE INDEX idx_manufacturer ON Product(Manufacturer);
11. Normalization (1NF, 2NF, 3NF)
11) Normalization - simple explanation and example

- Goal: Reduce redundancy and avoid update anomalies.

1NF (First Normal Form):


- Each cell holds atomic (single) values, and each record is unique.

2NF (Second Normal Form):


- In 1NF and every non-key attribute is fully functionally dependent on the
primary key.

3NF (Third Normal Form):


- In 2NF and no transitive dependency between non-key attributes.

Example: Suppose we have a table Orders(OrderID, CustomerName, CustomerPhone,


ProductID, ProductName)
- CustomerName and CustomerPhone depend only on Customer, not on OrderID; so we
split into Customer(CustomerID, Name, Phone) and Orders(OrderID, CustomerID,
ProductID).

This reduces duplication of customer details across orders.


12. Practical Exercises and Solutions
12) Practical Exercises and Solutions

Create table COMPDTLS and insert sample data:


CREATE TABLE COMPDTLS(
CompName VARCHAR(50) PRIMARY KEY,
RegDate DATE NOT NULL,
StockPrice FLOAT,
Country VARCHAR(30)
);

Insert rows:
INSERT INTO COMPDTLS VALUES ('GizmoWorks', '2019-10-21', 25, 'USA');
INSERT INTO COMPDTLS VALUES ('Canon', '2019-10-03', 65, 'Japan');
INSERT INTO COMPDTLS VALUES ('Hitachi', '2019-10-10', 15, 'India');

Sample queries and answers:


1) List details of all companies:
SELECT * FROM COMPDTLS;

2) Registration date of all companies:


SELECT CompName, RegDate FROM COMPDTLS;

3) Companies in Japan:
SELECT * FROM COMPDTLS WHERE Country = 'Japan';

4) Company with stock price 65:


SELECT CompName FROM COMPDTLS WHERE StockPrice = 65;

5) Companies of Japan or India:


SELECT * FROM COMPDTLS WHERE Country IN ('Japan','India');

6) Maximum stock price:


SELECT MAX(StockPrice) FROM COMPDTLS;

7) Average stock price:


SELECT AVG(StockPrice) FROM COMPDTLS;

8) Distinct countries:
SELECT DISTINCT Country FROM COMPDTLS;

9) Total number of countries:


SELECT COUNT(DISTINCT Country) FROM COMPDTLS;
10) Companies whose country ends with 'a':
SELECT CompName FROM COMPDTLS WHERE Country LIKE '%a';
13. Quick SQL Cheat Sheet
13) Quick SQL Cheat Sheet

- CREATE TABLE, DROP TABLE, ALTER TABLE


- INSERT INTO, UPDATE, DELETE
- SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ...
- JOIN types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN
- GROUP BY, HAVING, Aggregate functions: SUM, AVG, COUNT, MIN, MAX
- Constraints: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, NOT NULL
14. Glossary
14) Glossary

- Tuple: A row in a table.


- Attribute: A column in a table.
- Relation: A table in relational database terminology.
- Schema: Structure of the database.
- Instance: The data at a particular time.
- ACID: Atomicity, Consistency, Isolation, Durability.
Appendix: Example Schema Diagram

You might also like